perm filename DVITYP.TEX[WEB,ALS] blob sn#661607 filedate 1982-05-21 generic text, type T, neo UTF8
\input webhdr
% This program by D. E. Knuth is not copyrighted and can be used freely.
% But don't try to use it, since it hasn't been debugged yet, and won't be
% until TEX82 starts generating DVI files that can be used to test it!

% Here is TEX material that gets inserted after \input webhdr
\def\hang{\hangindent 3em\ \unskip\!}
\chcode@=13 \def@{\penalty999\ } % ties words together
\def\TEX{T\hbox{\hskip-.1667em\lower.424ex\hbox{E}\hskip-.125em X}}
\font b=cmr9 \def\mc{\:b} % medium caps for names like PASCAL
\def\PASCAL{{\mc PASCAL}}

\def\(#1){} % this is used to make module names sort themselves better
\def\9#1{} % this is used for sort keys in the index

\font D=cmtt at 15truept % font used in the title line below (only)
\font E=cmr7 at 14truept % font used in the title line below (only)

\def\title{DVI$\,$\lowercase{type}}
\def\contentspagenumber{101}
\def\topofcontents{\topspace 0pt
	\vfill
	\ctrline{\:E The {\:D DVItype} processor}
	\vskip 15pt
	\ctrline{(Version 0, not debugged)}
	\vfill}
\def\botofcontents{\vfill
	\ctrline{\ragged0\spaceskip0pt\xspaceskip0pt\baselineskip9pt
		\hbox par 5in{\:bThe preparation of this report
		was supported in part by the National Science
		Foundation under grants IST-7921977 and MCS-7723738;
		by Office of Naval Research grant N00014-81-K-0330;
		and by the IBM Corporation. `\TEX' is a registered
		trademark of the American Mathematical Society.}}}
\setcount0 \contentspagenumber
\topofcontents
\ctrline{(replace this page by the contents page printed later)}
\botofcontents
\mark{1}\eject

\N1.  Introduction.
The \.{DVItype} utility program reads binary device-independent (``\.{DVI}'')
files that are produced by document compilers such as \TEX, and converts them
into symbolic form. This program has two chief purposes: (1)@It can be used to
determine whether a \.{DVI} file is valid or invalid, when diagnosing
compiler errors; and (2)@it serves as an example of a program that reads
\.{DVI} files correctly, for system programmers who are developing
\.{DVI}-related software.

Goal number (2) needs perhaps a bit more explanation. Programs for
typesetting need to be especially careful about how they do arithmetic; if
rounding errors accumulate, margins won't be straight, vertical rules
won't line up, and so on. But if rounding is done everywhere, even in the
midst of words, there will be uneven spacing between the letters, and that
looks bad. Human eyes notice differences of a thousandth of an inch in the
positioning of lines that are close together; on low resolution devices,
where rounding produces effects four times as great as this, the problem
is especially critical. Experience has shown that unusual care is needed
even on high-resolution equipment; for example, a mistake in the sixth
significant hexadecimal place of a constant once led to a difficult-to-find
bug in some software for the Alphatype CRS, which has a resolution of 5333
pixels per inch (make that 5333.33333333 pixels per inch).  The document
compilers that generate \.{DVI} files make certain assumptions about the
arithmetic that will be used by \.{DVI}-reading software, and if these
assumptions are violated the results will be of inferior quality.
Therefore the present program is intended as a guide to proper procedure
in the critical places where a bit of subtlety is involved.

The first \.{DVItype} program was designed by David Fuchs in 1979, and it
went through several versions on different computers as the format of
\.{DVI} files was evolving to its present form.

The \\{banner} string defined here should be changed whenever \.{DVItype}
gets modified.

\Y\P\D \37$\\{banner}\S\.{\'This\ is\ DVItype,\ Version\ 0\'}$\C{printed when
the program starts}\par

\M2. This program is written in standard \PASCAL, except where it is necessary
to use extensions; for example, \.{DVItype} must read files whose names
are dynamically specified, and that would be impossible in pure \PASCAL.
All places where nonstandard constructions are used have been listed in
the index under ``system dependencies.''

One of the extensions to standard \PASCAL\ that we shall deal with is the
ability to move to a random place in a binary file; another is to
determine the length of a binary file. Such extensions are not necessary
for reading \.{DVI} files, and they are not important for efficiency
reasons either---an infrequently used program like \.{DVItype} does not
have to be efficient. But they are included there because of \.{DVItype}'s
r\accent'17ole as a model of a \.{DVI} reading routine, since other \.{DVI}
processors ought to be highly efficient. If \.{DVItype} is being used with
\PASCAL s for which random file positioning is not efficiently available,
the following definition should be changed from \\{true} to \\{false}; in such
cases, \.{DVItype} will simply read through the \.{DVI} file twice,
instead of doing so slightly more than once, so the running time for input
will increase by roughly a factor of two.

\Y\P\D \37$\\{random\_reading}\S\\{true}$\C{should we skip around in the
file?}\par

\M3. The binary input comes from \\{dvi\_file}, and the symbolic output is
written
on \PASCAL's standard \\{output} file. The term \\{print} is used instead of
\\{write} when this program writes on \\{output}, so that all such output
could easily be redirected if desired.

\Y\P\D \37$\\{print}(\#)\S\\{write}(\#)$\par
\P\D \37$\\{print\_ln}(\#)\S\\{write\_ln}(\#)$\par
\Y\P\4\&{program}\1\  \37$\\{DVI\_type}(\\{dvi\_file},\39\\{output})$;\6
\4\&{label} \37\X4:Labels in the outer block\X\6
\4\&{const} \37\X5:Constants in the outer block\X\6
\4\&{type} \37\X7:Types in the outer block\X\6
\4\&{var} \37\X9:Globals in the outer block\X\6
\4\&{procedure}\1\  \37\\{initialize};\C{this procedure gets things started
properly}\6
\4\&{var} \37\|i: \37\\{integer};\C{loop index for initializations}\2\6
\&{begin} \37$\\{print\_ln}(\\{banner})$;\6
\X10:Set initial values\X\6
\&{end};\par

\M4. If the program has to stop prematurely, it goes to the
`\\{final\_end}'.

\Y\P\D \37$\\{final\_end}=9999$\C{label for the end of it all}\par
\Y\P$\4\X4:Labels in the outer block\X\S$\6
\\{final\_end};\par
\U section 3.

\M5. The following parameters can be changed at compile time to extend or
reduce \.{DVItype}'s capacity.

\Y\P$\4\X5:Constants in the outer block\X\S$\6
$\\{max\_fonts}=100$;\C{maximum number of distinct fonts per \.{DVI} file}\6
$\\{max\_widths}=10000$;\C{maximum number of different characters among all
fonts}\6
$\\{line\_length}=80$;\C{bracketed lines of output will be at most this long}\6
$\\{terminal\_line\_length}=150$;\C{maximum number of characters input in a
single 	line of input from the terminal}\6
$\\{stack\_size}=100$;\C{\.{DVI} files shouldn't \\{push} beyond this depth}\6
$\\{name\_size}=1000$;\C{total length of all font file names}\6
$\\{name\_length}=50$;\C{a file name shouldn't be longer than this}\par
\U section 3.

\M6. Here are some macros for common programming idioms.

\Y\P\D \37$\\{incr}(\#)\S\#\K\#+1$\C{increase a variable by unity}\par
\P\D \37$\\{decr}(\#)\S\#\K\#-1$\C{decrease a variable by unity}\par
\P\D \37$\\{do\_nothing}\S$\C{empty statement}\par

\N7.  The character set.
Like all programs written with the  \.{WEB} system, \.{DVItype} can be
used with any character set. But it uses ascii code internally, because
the programming for portable input-output is easier when a fixed internal
code is used, and because \.{DVI} files use ascii code for file names
and certain other strings.

The next few modules of \.{DVItype} have therefore been copied from the
analogous ones in the \.{WEB} system routines. They have been considerably
simplified, since \.{DVItype} need not deal with the controversial
ascii codes less than \O40.

\Y\P$\4\X7:Types in the outer block\X\S$\6
$\\{ascii\_code}=\.{"\ "}\to\.{"\~"}$;\C{a subrange of the integers}\par
\A sections 8 and 18.
\U section 3.

\M8. The original \PASCAL\ compiler was designed in the late 60s, when six-bit
character sets were common, so it did not make provision for lower case
letters. Nowadays, of course, we need to deal with both upper and lower case
alphabets in a convenient way, especially in a program like \.{DVItype}.
So we shall assume that the \PASCAL\ system being used for \.{DVItype}
has a character set containing at least the standard visible characters
of ascii code (\.{"!"} through \.{"\~"}).

Some \PASCAL\ compilers use the original name \\{char} for the data type
associated with the characters in text files, while other \PASCAL s
consider \\{char} to be a 64-element subrange of a larger data type that has
some other name.  In order to accommodate this difference, we shall use
the name \\{text\_char} to stand for the data type of the characters in the
output file.  We shall also assume that \\{text\_char} consists of
the elements $\\{chr}(\\{first\_text\_char})$ through $\\{chr}(\\{last\_text\_
char})$,
inclusive. The following definitions should be adjusted if necessary.

\Y\P\D \37$\\{text\_char}\S\\{char}$\C{the data type of characters in text
files}\par
\P\D \37$\\{first\_text\_char}=0$\C{ordinal number of the smallest element of \\
{text\_char}}\par
\P\D \37$\\{last\_text\_char}=127$\C{ordinal number of the largest element of \\
{text\_char}}\par
\Y\P$\4\X7:Types in the outer block\X\mathrel{+}\S$\6
$\\{text\_file}=$\1\5
\&{packed} \37\&{file} \1\&{of}\5
\\{text\_char};\2\2\par

\M9. The \.{DVItype} processor converts between ascii code and
the user's external character set by means of arrays \\{xord} and \\{xchr}
that are analogous to \PASCAL's \\{ord} and \\{chr} functions.

\Y\P$\4\X9:Globals in the outer block\X\S$\6
\4\\{xord}: \37\&{array} $[\\{text\_char}]$ \1\&{of}\5
\\{ascii\_code};\C{specifies conversion of input characters}\2\6
\4\\{xchr}: \37\&{array} $[\\{ascii\_code}]$ \1\&{of}\5
\\{text\_char};\C{specifies conversion of output characters}\2\par
\A sections 19, 21, 22, 27, 30, 36, 38, 39, 42, 45, 54, 59, 60, 76, 78, and 90.
\U section 3.

\M10. Under our assumption that the visible characters of standard ascii are
all present, the following assignment statements initialize the
\\{xchr} array properly, without needing any system-dependent changes.

\Y\P$\4\X10:Set initial values\X\S$\6
$\\{xchr}[\O40]\K\.{\'\ \'}$;\5
$\\{xchr}[\O41]\K\.{\'!\'}$;\5
$\\{xchr}[\O42]\K\.{\'"\'}$;\5
$\\{xchr}[\O43]\K\.{\'\#\'}$;\5
$\\{xchr}[\O44]\K\.{\'\$\'}$;\5
$\\{xchr}[\O45]\K\.{\'\%\'}$;\5
$\\{xchr}[\O46]\K\.{\'\&\'}$;\5
$\\{xchr}[\O47]\K\.{\'\'}\.{\'\'}$;\6
$\\{xchr}[\O50]\K\.{\'(\'}$;\5
$\\{xchr}[\O51]\K\.{\')\'}$;\5
$\\{xchr}[\O52]\K\.{\'*\'}$;\5
$\\{xchr}[\O53]\K\.{\'+\'}$;\5
$\\{xchr}[\O54]\K\.{\',\'}$;\5
$\\{xchr}[\O55]\K\.{\'-\'}$;\5
$\\{xchr}[\O56]\K\.{\'.\'}$;\5
$\\{xchr}[\O57]\K\.{\'/\'}$;\6
$\\{xchr}[\O60]\K\.{\'0\'}$;\5
$\\{xchr}[\O61]\K\.{\'1\'}$;\5
$\\{xchr}[\O62]\K\.{\'2\'}$;\5
$\\{xchr}[\O63]\K\.{\'3\'}$;\5
$\\{xchr}[\O64]\K\.{\'4\'}$;\5
$\\{xchr}[\O65]\K\.{\'5\'}$;\5
$\\{xchr}[\O66]\K\.{\'6\'}$;\5
$\\{xchr}[\O67]\K\.{\'7\'}$;\6
$\\{xchr}[\O70]\K\.{\'8\'}$;\5
$\\{xchr}[\O71]\K\.{\'9\'}$;\5
$\\{xchr}[\O72]\K\.{\':\'}$;\5
$\\{xchr}[\O73]\K\.{\';\'}$;\5
$\\{xchr}[\O74]\K\.{\'<\'}$;\5
$\\{xchr}[\O75]\K\.{\'=\'}$;\5
$\\{xchr}[\O76]\K\.{\'>\'}$;\5
$\\{xchr}[\O77]\K\.{\'?\'}$;\6
$\\{xchr}[\O100]\K\.{\'@\'}$;\5
$\\{xchr}[\O101]\K\.{\'A\'}$;\5
$\\{xchr}[\O102]\K\.{\'B\'}$;\5
$\\{xchr}[\O103]\K\.{\'C\'}$;\5
$\\{xchr}[\O104]\K\.{\'D\'}$;\5
$\\{xchr}[\O105]\K\.{\'E\'}$;\5
$\\{xchr}[\O106]\K\.{\'F\'}$;\5
$\\{xchr}[\O107]\K\.{\'G\'}$;\6
$\\{xchr}[\O110]\K\.{\'H\'}$;\5
$\\{xchr}[\O111]\K\.{\'I\'}$;\5
$\\{xchr}[\O112]\K\.{\'J\'}$;\5
$\\{xchr}[\O113]\K\.{\'K\'}$;\5
$\\{xchr}[\O114]\K\.{\'L\'}$;\5
$\\{xchr}[\O115]\K\.{\'M\'}$;\5
$\\{xchr}[\O116]\K\.{\'N\'}$;\5
$\\{xchr}[\O117]\K\.{\'O\'}$;\6
$\\{xchr}[\O120]\K\.{\'P\'}$;\5
$\\{xchr}[\O121]\K\.{\'Q\'}$;\5
$\\{xchr}[\O122]\K\.{\'R\'}$;\5
$\\{xchr}[\O123]\K\.{\'S\'}$;\5
$\\{xchr}[\O124]\K\.{\'T\'}$;\5
$\\{xchr}[\O125]\K\.{\'U\'}$;\5
$\\{xchr}[\O126]\K\.{\'V\'}$;\5
$\\{xchr}[\O127]\K\.{\'W\'}$;\6
$\\{xchr}[\O130]\K\.{\'X\'}$;\5
$\\{xchr}[\O131]\K\.{\'Y\'}$;\5
$\\{xchr}[\O132]\K\.{\'Z\'}$;\5
$\\{xchr}[\O133]\K\.{\'[\'}$;\5
$\\{xchr}[\O134]\K\.{\'\\\'}$;\5
$\\{xchr}[\O135]\K\.{\']\'}$;\5
$\\{xchr}[\O136]\K\.{\'\↑\'}$;\5
$\\{xchr}[\O137]\K\.{\'\_\'}$;\6
$\\{xchr}[\O140]\K\.{\'\`\'}$;\5
$\\{xchr}[\O141]\K\.{\'a\'}$;\5
$\\{xchr}[\O142]\K\.{\'b\'}$;\5
$\\{xchr}[\O143]\K\.{\'c\'}$;\5
$\\{xchr}[\O144]\K\.{\'d\'}$;\5
$\\{xchr}[\O145]\K\.{\'e\'}$;\5
$\\{xchr}[\O146]\K\.{\'f\'}$;\5
$\\{xchr}[\O147]\K\.{\'g\'}$;\6
$\\{xchr}[\O150]\K\.{\'h\'}$;\5
$\\{xchr}[\O151]\K\.{\'i\'}$;\5
$\\{xchr}[\O152]\K\.{\'j\'}$;\5
$\\{xchr}[\O153]\K\.{\'k\'}$;\5
$\\{xchr}[\O154]\K\.{\'l\'}$;\5
$\\{xchr}[\O155]\K\.{\'m\'}$;\5
$\\{xchr}[\O156]\K\.{\'n\'}$;\5
$\\{xchr}[\O157]\K\.{\'o\'}$;\6
$\\{xchr}[\O160]\K\.{\'p\'}$;\5
$\\{xchr}[\O161]\K\.{\'q\'}$;\5
$\\{xchr}[\O162]\K\.{\'r\'}$;\5
$\\{xchr}[\O163]\K\.{\'s\'}$;\5
$\\{xchr}[\O164]\K\.{\'t\'}$;\5
$\\{xchr}[\O165]\K\.{\'u\'}$;\5
$\\{xchr}[\O166]\K\.{\'v\'}$;\5
$\\{xchr}[\O167]\K\.{\'w\'}$;\6
$\\{xchr}[\O170]\K\.{\'x\'}$;\5
$\\{xchr}[\O171]\K\.{\'y\'}$;\5
$\\{xchr}[\O172]\K\.{\'z\'}$;\5
$\\{xchr}[\O173]\K\.{\'\{\'}$;\5
$\\{xchr}[\O174]\K\.{\'|\'}$;\5
$\\{xchr}[\O175]\K\.{\'\}\'}$;\5
$\\{xchr}[\O176]\K\.{\'\~\'}$;\par
\A sections 11, 28, 40, 55, and 91.
\U section 3.

\M11. The following system-independent code makes the \\{xord} array contain a
suitable inverse to the information in \\{xchr}.

\Y\P$\4\X10:Set initial values\X\mathrel{+}\S$\6
\&{for} $\|i\K\\{first\_text\_char}\mathrel{\&{to}}\\{last\_text\_char}$ \1\&
{do}\5
$\\{xord}[\\{chr}(\|i)]\K\O40$;\2\6
\&{for} $\|i\K\.{"\ "}\mathrel{\&{to}}\.{"\~"}$ \1\&{do}\5
$\\{xord}[\\{xchr}[\|i]]\K\|i$;\2\par

\N12.  Device-independent file format.
Before we get into the details of \.{DVItype}, we need to know exactly
what \.{DVI} files are. The form of such files was designed by David R.
Fuchs in 1979. Almost any reasonable device can be driven by a program
that takes \.{DVI} files as input, and dozens of such \.{DVI}-to-whatever
programs have been written. Thus, it is possible to print the output of
document compilers like \TEX\ on many different kinds of equipment.

A \.{DVI} file is a stream of 8-bit bytes, which may be regarded as a
series of commands in a machine-like language. The first byte of each command
is the operation code, and this code is followed by zero or more bytes
that provide parameters to the command. The parameters themselves may consist
of several consecutive bytes; for example, the `\\{set\_rule}' command has two
parameters, each of which is four bytes long. Parameters are usually
regarded as nonnegative integers; but four-byte-long parameters,
and shorter parameters that denote distances, can be
either positive or negative. Such parameters are given in two's complement
notation. For example, a two-byte-long distance parameter has a value between
$-2↑{15}$ and $2↑{15}-1$.

A \.{DVI} file consists of a sequence of one or more ``pages,'' followed by
a ``postamble.'' A ``page'' consists of a \\{bop} command, followed by any
number
of other commands that tell where characters are to be placed on a physical
page, followed by an \\{eop} command. The pages appear in the order that \TEX\
generated them. If we ignore \\{nop} commands (which are allowed between
any two commands in the file), each \\{eop} command is immediately followed by
a \\{bop} command, or by a \\{pst} command; in the latter case, there are no
more pages in the file, and the remaining bytes form the postamble.
Further details about the postamble will be explained later.

Some parameters in \.{DVI} commands are ``pointers.'' These are four-byte
quantities that give the location number of some other byte in the file;
the first byte is number@0, then comes number@1, and so on. For example,
one of the parameters of a \\{bop} command points to the previous \\{bop};
this makes it feasible to read the pages in backwards order, in case you
are producing output on devices that stack their output face up. If the
first page on a \.{DVI} file occupies bytes 0 to 99, and if the second
page occupies bytes 100 to 299, then the \\{bop} that starts in byte 100
points to 0 and the \\{bop} thats starts in byte 300 points to 100. (The
first \\{bop}, i.e., the one that starts in byte 0, has a pointer of $-1$.)


\M13. The \.{DVI} format is intended to be both compact and easily interpreted
by a machine. Compactness is achieved by making most of the information
implicit instead of explicit; when a \.{DVI}-reading program reads the
commands for a page, it keeps track of several quantities: (a)@The current
font \|f is an integer; this value is changed only
by \\{fnt} and \\{fnt\_num} commands. (b)@The current position on the page
is given by two numbers called the horizontal and vertical coordinates,
\|h and \|v. Both coordinates are zero at the upper left corner of the page;
moving to the right corresponds to increasing the horizontal coordinate, and
moving down corresponds to increasing the vertical coordinate. Thus, the
coordinates are essentially Cartesian, except that vertical directions are
flipped; the Cartesian version of $(\|h,\|v)$ would be $(\|h,-\|v)$.  (c)@The
current spacing amounts are given by four numbers \|w, \|x, \|y, and \|z,
where \|w and@\|x are used for horizontal spacing and where \|y and@\|z
are used for vertical spacing. (d)@There is a stack containing
$(\|h,\|v,\|w,\|x,\|y,\|z)$ values; the \.{DVI} commands \\{push} and \\{pop}
are used to
change the current level of operation. Note that the current font@\|f is
not pushed and popped; the stack contains only information about
positioning.

The values of \|h, \|v, \|w, \|x, \|y, and \|z are signed integers having up
to 32 bits, including the sign. Since they represent physical distances,
there is a small unit of measurement such that increasing \|h by@1 means
moving a certain tiny distance to the right. The actual unit of
measurement is variable, as explained below.


\M14. Here is list of all the commands that may appear in a \.{DVI} file. With
each command we give its symbolic name (e.g., \\{bop}), its opcode byte
(e.g., 129), and its parameters (if any). The parameters are followed
by a bracketed number telling how many bytes they occupy; for example,
`$\|p[4]$' means that parameter \|p is four bytes long.

\yskip\hang\\{set\_char\_0} 0. Typeset character number@0 from font@\|f
such that the reference point of the character is at $(\|h,\|v)$. Then
increase \|h by the width of that character. Note that a character may
have zero or negative width, so one cannot be sure that \|h will advance
after this command; but \|h usually does increase.

\yskip\hang\\{set\_char\_1} through \\{set\_char\_127} (opcodes 1 to 127).
Do the operations of \\{set\_char\_0}, but use the appropriate character number
instead of character@0.

\yskip\hang\\{set1} 128 $\|c[1]$. Same as \\{set\_char\_0}, except that
character
number@\|c is typeset. \TEX82 uses this command for characters in the
range $128\L\|c<256$.

\yskip\hang\\{set2} 129 $\|c[2]$. Same as \\{set1}, except that@\|c is two
bytes long, so it is in the range $0\L\|c<65536$. \TEX82 never uses this
command, which is intended for processors that deal with oriental languages;
but \.{DVItype} will allow character codes greater than 255, assuming that
they all have the same width as character 256.

\yskip\hang\\{set3} 130 $\|c[3]$. Same as \\{set1}, except that@\|c is three
bytes long, so it can be as large as $2↑{24}-1$. Not even the Chinese
language has this many characters, but this command might prove useful
in some yet unforeseen way.

\yskip\hang\\{set4} 131 $\|c[4]$. Same as \\{set1}, except that@\|c is four
bytes long, possibly even negative. Imagine that.

\yskip\hang\\{set\_rule} 132 $\|a[4]$ $\|b[4]$. Typeset a solid black rectangle
of height \|a and width \|b, with its bottom left corner at $(\|h,\|v)$. Then
set $\|h\K\|h+\|b$. If either $\|a\L0$ or $\|b\L0$, nothing should be typeset.
Note
that if $\|b<0$, the value of \|h will decrease even though nothing else
happens.
Programs that typeset from \.{DVI} files should be careful to make the rules
line up carefully with digitized characters, as explained in connection with
the \\{rule\_pixels} subroutine below.

\yskip\hang\\{put1} 133 $\|c[1]$. Typeset character number@\|c from font@\|f
such that the reference point of the character is at $(\|h,\|v)$. (The `put'
commands are exactly like the `set' commands, except that they simply put out a
character or a rule without moving the reference point afterwards.)

\yskip\hang\\{put2} 134 $\|c[2]$. Same as \\{set2}, except that \|h is not
changed.

\yskip\hang\\{put3} 135 $\|c[3]$. Same as \\{set3}, except that \|h is not
changed.

\yskip\hang\\{put4} 136 $\|c[4]$. Same as \\{set4}, except that \|h is not
changed.

\yskip\hang\\{put\_rule} 137 $\|a[4]$ $\|b[4]$. Same as \\{set\_rule}, except
that
\|h is not changed.

\yskip\hang\\{nop} 138. No operation, do nothing. Any number of \\{nop}'s
may occur between \.{DVI} commands, but a \\{nop} cannot be inserted between
a command and its parameters or between two parameters.

\yskip\hang\\{bop} 139 $c↓0[4]$ $c↓1[4]$ $\ldots$ $c↓9[4]$ $p[4]$. Beginning
of a page: Set $(\|h,\|v,\|w,\|x,\|y,\|z)\K(0,0,0,0,0,0)$ and set the stack
empty. Set
the current font \|f to an undefined value.  The ten $c↓i$ parameters can
be used to identify pages, if a user wants to print only part of a \.{DVI}
file; \TEX82 gives them the values of \.{\\count0} $\ldots$ \.{\\count9}
at the time \.{\\shipout} was invoked for this page.  The parameter \|p
points to the previous \\{bop} command in the file, where the first \\{bop}
has $p=-1$.

\yskip\hang\\{eop} 140.  End of page: Print what you have read since the
previous \\{bop}. At this point the stack should be empty. (The \.{DVI}-reading
programs that drive most output devices will have kept a buffer of the
material that appears on the page that has just ended. This material is
largely, but not entirely, in order by \|v coordinate and (for fixed \|v) by
\|h@coordinate; so it usually needs to be sorted into some order that is
appropriate for the device in question. \.{DVItype} does not do such sorting.)

\yskip\hang\\{push} 141. Push the current values of $(\|h,\|v,\|w,\|x,\|y,\|z)$
onto the
top of the stack; do not change any of these values. Note that \|f is
not pushed.

\yskip\hang\\{pop} 142. Pop the top six values off of the stack and assign
them to $(\|h,\|v,\|w,\|x,\|y,\|z)$. The number of pops should never exceed the
number
of pushes, since it would be highly embarrassing if the stack were empty
at the time of a \\{pop} command.

\yskip\hang\\{right1} 143 $\|b[1]$. Set $\|h\K\|h+\|b$, i.e., move right \|b
units.
The parameter is a signed number in two's complement notation, $-128\L\|b<128$;
if $\|b<0$, the reference point actually moves left.

\yskip\hang\\{right2} 144 $\|b[2]$. Same as \\{right1}, except that \|b is a
two-byte quantity in the range $-32768\L\|b<32768$.

\yskip\hang\\{right3} 145 $\|b[3]$. Same as \\{right1}, except that \|b is a
three-byte quantity in the range $\hbox{$-2↑{23}$}\L\|b<\hbox{$2↑{23}$}$.

\yskip\hang\\{right4} 146 $\|b[4]$. Same as \\{right1}, except that \|b is a
four-byte quantity in the range $\hbox{$-2↑{31}$}\L\|b<\hbox{$2↑{31}$}$.

\yskip\hang\\{w0} 147. Set $\|h\K\|h+\|w$; i.e., move right \|w units. With
luck,
this parameterless command will usually suffice, because the same kind of
motion
will occur several times in succession; the following commands explain how
\|w gets particular values.

\yskip\hang\\{w1} 148 $\|b[1]$. Set $\|w\K\|b$ and $\|h\K\|h+\|b$. The value of
\|b is a
signed quantity in two's complement notation, $-128\L\|b<128$. This command
changes the current \|w@spacing and moves right by \|b.

\yskip\hang\\{w2} 149 $\|b[2]$. Same as \\{w1}, but \|b is a two-byte-long
parameter, $-32768\L\|b<32768$.

\yskip\hang\\{w3} 150 $\|b[3]$. Same as \\{w1}, but \|b is a three-byte-long
parameter, $\hbox{$-2↑{23}$}\L\|b<\hbox{$2↑{23}$}$.

\yskip\hang\\{w4} 151 $\|b[4]$. Same as \\{w1}, but \|b is a four-byte-long
parameter, $\hbox{$-2↑{31}$}\L\|b<\hbox{$2↑{31}$}$.

\yskip\hang\\{x0} 152. Set $\|h\K\|h+\|x$; i.e., move right \|x units. The `\|
x'
commands are like the `\|w' commands except that they involve \|x instead
of \|w.

\yskip\hang\\{x1} 153 $\|b[1]$. Set $\|x\K\|b$ and $\|h\K\|h+\|b$. The value of
\|b is a
signed quantity in two's complement notation, $-128\L\|b<128$. This command
changes the current \|x@spacing and moves right by \|b.

\yskip\hang\\{x2} 154 $\|b[2]$. Same as \\{x1}, but \|b is a two-byte-long
parameter, $-32768\L\|b<32768$.

\yskip\hang\\{x3} 155 $\|b[3]$. Same as \\{x1}, but \|b is a three-byte-long
parameter, $\hbox{$-2↑{23}$}\L\|b<\hbox{$2↑{23}$}$.

\yskip\hang\\{x4} 156 $\|b[4]$. Same as \\{x1}, but \|b is a four-byte-long
parameter, $\hbox{$-2↑{31}$}\L\|b<\hbox{$2↑{31}$}$.

\yskip\hang\\{down1} 157 $\|a[1]$. Set $\|v\K\|v+\|a$, i.e., move down \|a
units.
The parameter is a signed number in two's complement notation, $-128\L\|a<128$;
if $\|a<0$, the reference point actually moves up.

\yskip\hang\\{down2} 158 $\|a[2]$. Same as \\{down1}, except that \|a is a
two-byte quantity in the range $-32768\L\|a<32768$.

\yskip\hang\\{down3} 159 $\|a[3]$. Same as \\{down1}, except that \|a is a
three-byte quantity in the range $\hbox{$-2↑{23}$}\L\|a<\hbox{$2↑{23}$}$.

\yskip\hang\\{down4} 160 $\|a[4]$. Same as \\{down1}, except that \|a is a
four-byte quantity in the range $\hbox{$-2↑{31}$}\L\|a<\hbox{$2↑{31}$}$.

\yskip\hang\\{y0} 161. Set $\|v\K\|v+\|y$; i.e., move down \|y units. With
luck,
this parameterless command will usually suffice, because the same kind of
motion
will occur several times in succession; the following commands explain how
\|y gets particular values.

\yskip\hang\\{y1} 162 $\|a[1]$. Set $\|y\K\|a$ and $\|v\K\|v+\|a$. The value of
\|a is a
signed quantity in two's complement notation, $-128\L\|a<128$. This command
changes the current \|y@spacing and moves down by \|a.

\yskip\hang\\{y2} 163 $\|a[2]$. Same as \\{y1}, but \|a is a two-byte-long
parameter, $-32768\L\|a<32768$.

\yskip\hang\\{y3} 164 $\|a[3]$. Same as \\{y1}, but \|a is a three-byte-long
parameter, $\hbox{$-2↑{23}$}\L\|a<\hbox{$2↑{23}$}$.

\yskip\hang\\{y4} 165 $\|a[4]$. Same as \\{y1}, but \|a is a four-byte-long
parameter, $\hbox{$-2↑{31}$}\L\|a<\hbox{$2↑{31}$}$.

\yskip\hang\\{z0} 166. Set $\|v\K\|v+\|z$; i.e., move down \|z units. The `\|z'
commands
are like the `\|y' commands except that they involve \|z instead of \|y.

\yskip\hang\\{z1} 167 $\|a[1]$. Set $\|z\K\|a$ and $\|v\K\|v+\|a$. The value of
\|a is a
signed quantity in two's complement notation, $-128\L\|a<128$. This command
changes the current \|z@spacing and moves down by \|a.

\yskip\hang\\{z2} 168 $\|a[2]$. Same as \\{z1}, but \|a is a two-byte-long
parameter, $-32768\L\|a<32768$.

\yskip\hang\\{z3} 169 $\|a[3]$. Same as \\{z1}, but \|a is a three-byte-long
parameter, $\hbox{$-2↑{23}$}\L\|a<\hbox{$2↑{23}$}$.

\yskip\hang\\{z4} 170 $\|a[4]$. Same as \\{z1}, but \|a is a four-byte-long
parameter, $\hbox{$-2↑{31}$}\L\|a<\hbox{$2↑{31}$}$.

\yskip\hang\\{fnt\_num\_0} 171. Set $\|f\K0$.

\yskip\hang\\{fnt\_num\_1} through \\{fnt\_num\_63} (opcodes 172 to 234). Set
$\|f\K1$, $\ldotss$, $\|f\K63$, respectively.

\yskip\hang\\{fnt1} 235 $\|n[1]$. Set $\|f\K\|n$. \TEX82 uses this command for
font
numbers in the range $64\L\|n<256$.

\yskip\hang\\{fnt2} 236 $\|n[2]$. Same as \\{fnt1}, except that@\|n is two
bytes long, so it is in the range $0\L\|n<65536$. \TEX82 never generates this
command, but large font numbers may prove useful for specifications of
color or texture, or they may be used for special fonts that have fixed
numbers in some external coding scheme.

\yskip\hang\\{fnt3} 237 $\|n[3]$. Same as \\{fnt1}, except that@\|n is three
bytes long, so it can be as large as $2↑{24}-1$.

\yskip\hang\\{fnt4} 238 $\|n[4]$. Same as \\{fnt1}, except that@\|n is four
bytes long; this is for the really big font numbers. The value $-1$
is forbidden, so the legal values of \|f are $-2↑{31}\L f<-1$ and
$0\L f<2↑{31}-1$.

\yskip\hang\\{xxx} 239 $\|m[1]$ $\|x[\|m]$. This command is undefined in
general; it functions as an $(m+2)$-byte \\{nop} unless special \.{DVI}-reading
programs are being used. \TEX82 generates this command when an \.{\\xsend}
appears, setting \|m to the number of bytes being sent. It is recommended that
\|x be a string having the form of a keyword followed by possible parameters
relevant to that keyword. Examples: $\|x=\.{\'halftone\ fig22\'}$ could mean
``insert a halftone from file fig22, with its reference point at $(\|h,\|v)$'';
$\|x=\.{\'leftend\ 2\'}$ and an appearance elsewhere of $\|x=\.{\'rightend\ 2\'
}$ could mean
``draw a straight line from the left $(\|h,\|v)$ position to the right one''
(where the `\.2' is an identifier to distinguish this straight line
from others in the file); $\|x=\.{\'message\ Foo\'}$ could mean ``display `\.
{Foo}'
on the console of the printing device''; and so on. The command does not
change any of the status values \|f, \|h, \|v, \|w, \|x, \|y, \|z or the stack.

\yskip\hang\\{pst} 240. Beginning of the postamble, see below.

\yskip\noindent Commands 241--255 are undefined at the present time.


\M15. \P\D \37$\\{set\_char\_0}=0$\C{typeset character 0 and move right}\par
\P\D \37$\\{set1}=128$\C{typeset a character and move right}\par
\P\D \37$\\{set\_rule}=132$\C{typeset a rule and move right}\par
\P\D \37$\\{put1}=133$\C{typeset a character}\par
\P\D \37$\\{put\_rule}=137$\C{typeset a rule}\par
\P\D \37$\\{nop}=138$\C{no operation}\par
\P\D \37$\\{bop}=139$\C{beginning of page}\par
\P\D \37$\\{eop}=140$\C{ending of page}\par
\P\D \37$\\{push}=141$\C{save the current positions}\par
\P\D \37$\\{pop}=142$\C{restore previous positions}\par
\P\D \37$\\{right1}=143$\C{move right}\par
\P\D \37$\\{w0}=147$\C{move right by \|w}\par
\P\D \37$\\{w1}=148$\C{move right and set \|w}\par
\P\D \37$\\{x0}=152$\C{move right by \|x}\par
\P\D \37$\\{x1}=153$\C{move right and set \|x}\par
\P\D \37$\\{down1}=157$\C{move down}\par
\P\D \37$\\{y0}=161$\C{move down by \|y}\par
\P\D \37$\\{y1}=162$\C{move down and set \|y}\par
\P\D \37$\\{z0}=166$\C{move down by \|z}\par
\P\D \37$\\{z1}=167$\C{move down and set \|z}\par
\P\D \37$\\{fnt\_num\_0}=171$\C{set current font to 0}\par
\P\D \37$\\{fnt1}=235$\C{set current font}\par
\P\D \37$\\{xxx}=239$\C{extension to \.{DVI} primitives}\par
\P\D \37$\\{pst}=240$\C{postamble}\par
\P\D \37$\\{undefined\_commands}\S241,\39242,\39243,\39244,\39245,\39246,\3
9247,\39248,\39249,\39250,\39251,\39252,\39253,\39254,\39255$\par

\M16. The last page in a \.{DVI} file is followed by `\\{pst}'; this command
introduces the postamble, which summarizes important facts that \TEX\ has
accumulated about the file. The postamble has the form
$$\hbox{$\|p[4]$ $\|n[4]$ $\|d[4]$ $\|m[4]$ $\|l[4]$ $\|u[4]$ $\|s[2]$ $\|t[2]$
$\langle\,$font definitions$\,\rangle$
$(-1)[4]$ $\|q[4]$ $\|i[1]$ 223's$[\G4]$}$$
Here \|p is a pointer to the final \\{bop} in the file. The next two
parameters,
\|n and \|d, are positive integers that define the units of measurement;
they are the numerator and denominator of a fraction by which all dimensions
in the \.{DVI} file could be multiplied in order to get lengths in units
of $10↑{-7}$ meters. \.{DVI}-reading programs should do their basic arithmetic
with the unscaled units that appear in the file, multiplying by a conversion
factor only at the last step before outputting to another device; in this way,
rounding errors will not accumulate and there will be perfect agreement with
the assumptions of the document compiler that generated the \.{DVI} file.

The next parameter, \|m, is \TEX's \.{\\mag} parameter, i.e., 1000 times the
desired magnification. The actual fraction by which dimensions are multiplied
is therefore $\\{mn}/1000\|d$. Fancy \.{DVI}-reading programs allow users to
override the \|m setting when a \.{DVI} file is being printed.

Parameters \|l and \|u give respectively the height-plus-depth of the tallest
page and the width of the widest page, in the same units as other dimensions
of the file. These numbers might be used by a \.{DVI}-reading program to
position individual ``pages'' on large sheets of film or paper.

Parameter \|s is the maximum stack depth (i.e., the excess of \\{push} commands
over \\{pop} commands) needed to process this file. Then comes \|t, the total
number of pages (\\{bop} commands) present.

The postamble continues with font definitions, which are any number of
specifications having the form
$$\hbox{$\|f[4]$ $c↓f[4]$ $s↓f[4]$ $a↓f[1]$ $l↓f[1]$ $n↓f[a↓f+l↓f]$.}$$
The first parameter in a font definition is the font number, $f$; this
must be different from $-1$ and distinct from the font numbers in other
definitions. (Note that the font definitions are followed by the four-byte
value $-1$, so it will be clear when the definitions have ended.) The
next parameter, $c↓f$, is the check sum that \TEX\ found in the \.{TFM}
file for this font; it should match the check sum of the font found by
programs that read this \.{DVI} file. (Otherwise the font information has
changed since the time the \.{DVI} file was generated, and things may no
longer line up properly.)

Parameter $s↓f$ contains a fixed-point scale factor that is applied to the
character widths in font \|f; font dimensions in \.{TFM} files and other font
files are relative to this quantity. \TEX82 calls this the ``at size'' of
the font, since a particular font can be used at several different
magnifications.  The value of $s↓f$ should be positive and less than
$2↑{27}$. It is given in the same units as the other dimensions of the file.

The remaining part of the font definition gives the external name of the font,
which is an ascii string of length $a↓f+l↓f$. The number $a↓f$ is the length
of the ``area'' or directory, and $l↓f$ is the length of the font name itself;
the standard local system font area is supposed to be used when $a↓f=0$.
The $n↓f$ field contains the area in its first $a↓f$ bytes.


\M17. The last part of the postamble, following the phony font number
$-1$, contains \|q, a pointer to the \\{pst} command that started the
postamble.  An identification byte, \|i, comes next; currently this byte
is always set to@2. (Some day we will set $\|i=3$, when \.{DVI} format
makes another incompatible change---perhaps in 1992.)

Following the \|i byte there are four or more bytes that are all equal to
the decimal number 223 (i.e., \O337 in octal). \TEX\ puts out four to seven of
these trailing bytes, until the total length of the file is a multiple of
four bytes, since this works out best on machines that pack four bytes per
word; but any number of 223's is allowed, as long as there are at least four
of them. In effect, 223 is a sort of signature that is added at the very end.

This curious way to finish off a \.{DVI} file makes it feasible for
\.{DVI}-reading programs to find the postamble first, on most computers,
even though \TEX\ wants to write the postamble last. Most operating
systems permit random access to individual words or bytes of a file, so
the \.{DVI} reader starts at the end and skips backwards over the 223's
until finding the identification byte. Then it backs up four bytes, reads
\|q, and goes to byte \|q of the file. This byte should, of course,
contain the value 240 (\\{pst}); now the postamble can be read, so the
\.{DVI} reader discovers all the information needed for typesetting the
pages. Note that it is also possible to skip through the \.{DVI} file at
reasonably high speed to locate a particular page, if that proves
desirable.

The reason for reading the postamble first is that the \.{DVI} reader must
know the widths of characters, in order to find out where things go on a page;
and it needs to know the names of the fonts, so that it can get their widths
from a \.{TFM} file or from some other kind of font-information file.
The reason for writing the postamble last is that \TEX\ can't put out all
the font names until it has finished generating the pages of the \.{DVI}
file, since new fonts can occur anywhere in a \TEX\ job; and the alternative
of sprinkling font definitions throughout a \.{DVI} file is unattractive,
since that would make it necessary to read the whole file even when
printing only one page. Furthermore, we wouldn't want to copy the
information in the first part of a \.{DVI} file to the end of another file
that begins with the postamble information, since the first part of a
\.{DVI} file is typically quite long.

Unfortunately, however, standard \PASCAL\ does not include the ability to
access a random position in a file, or even to determine the length of a file.
Almost all systems nowadays provide the necessary capabilities, so \.{DVI}
format has been designed to work most efficiently with modern operating
systems.
As noted above, \.{DVItype} will limit itself to the restrictions of standard
\PASCAL\ if \\{random\_reading} is defined to be \\{false}.

\Y\P\D \37$\\{id\_byte}=2$\C{identifies the kind of \.{DVI} files described
here}\par

\N18.  Input from binary files.
We have seen that a \.{DVI} file is a sequence of 8-bit bytes. The bytes
appear physically in what is called a `\!\&{packed} \&{file} \&{of}
$0\to255$\unskip' in \PASCAL\ lingo.

Packing is system dependent, and many \PASCAL\ systems fail to implement
such files in a sensible way (at least, from the viewpoint of producing
good production software).  For example, some systems treat all
byte-oriented files as text, looking for end-of-line marks and such
things. Therefore some system-dependent code is often needed to deal with
binary files, even though most of the program in this section of
\.{DVItype} is written in standard \PASCAL.

One common way to solve the problem is to consider files of \\{integer}
numbers, and to convert an integer in the range $-2↑{31}\L x<2↑{31}$ to
a sequence of four bytes $(a,b,c,d)$ using the following code, which
avoids the controversial integer division of negative numbers:
$$\vbox{\halign{#\hfil\cr
 \&{if} $\|x\G0$ \&{then} $\|a\K\|x\mathbin{\&{div}}\O100000000$\cr
  \&{else}  \&{begin} $\|x\K(\|x+\O10000000000)+\O10000000000$; $\|a\K\|
x\mathbin{\&{div}}\O100000000+128$;\cr
\quad  \&{end} \cr
$\|x\K\|x\mathbin{\&{mod}}\O100000000$;\cr
$\|b\K\|x\mathbin{\&{div}}\O200000$; $\|x\K\|x\mathbin{\&{mod}}\O200000$;\cr
$\|c\K\|x\mathbin{\&{div}}\O400$; $\|d\K\|x\mathbin{\&{mod}}\O400$;\cr}}$$
The four bytes are then kept in a buffer and output one by one. (On 36-bit
computers, an additional division by 16 is necessary at the beginning.
Another way to separate an integer into four bytes is to use/abuse
\PASCAL's variant records, storing an integer and fetching bytes that are
packed in the same place; {\sl caveat implementor!\/}) It is also desirable
in some cases to read a hundred or so integers at a time, maintaining a
larger buffer.

We shall stick to simple \PASCAL\ in this program, for reasons of clarity,
even if such simplicity is sometimes unrealistic.

\Y\P$\4\X7:Types in the outer block\X\mathrel{+}\S$\6
$\\{eight\_bits}=0\to255$;\C{unsigned one-byte quantity}\6
$\\{byte\_file}=$\1\5
\&{packed} \37\&{file} \1\&{of}\5
\\{eight\_bits};\C{files that contain binary data}\2\2\par

\M19. The program deals with two binary file variables: \\{dvi\_file} is the
main
input file that we are translating into symbolic form, and \\{tfm\_file} is
the current font metric file from which character-width information is
being read.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{dvi\_file}: \37\\{byte\_file};\C{the stuff we are \.{DVI}typing}\6
\4\\{tfm\_file}: \37\\{byte\_file};\C{a font metric file}\par

\M20. To prepare these files for input, we \\{reset} them. An extension of
\PASCAL\ 
is needed in the case of \\{tfm\_file}, since we want to associate it with
external files whose names are specified dynamically (i.e., not known
at compile time). The following code assumes that `$\\{reset}(\|f,\|s)$' does
this,
when \|f is a file variable and \|s is a string variable that specifies
the file name. If $\\{eof}(\|f)$ is true immediately after $\\{reset}(\|f,\|s)$
has acted,
we assume that no file named \|s is accessible.

\Y\P\4\&{procedure}\1\  \37\\{open\_dvi\_file};\C{prepares to read packed bytes
in \\{dvi\_file}}\2\6
\&{begin} \37$\\{reset}(\\{dvi\_file})$;\5
$\\{cur\_loc}\K0$;\6
\&{end};\7
\4\&{procedure}\1\  \37\\{open\_tfm\_file};\C{prepares to read packed bytes in
\\{tfm\_file}}\2\6
\&{begin} \37$\\{reset}(\\{tfm\_file},\39\\{cur\_name})$;\6
\&{end};\par

\M21. If you looked carefully at the preceding code, you probably asked,
``What are \\{cur\_loc} and \\{cur\_name}?'' Good question. They're global
variables: \\{cur\_loc} is the number of the byte about to be read next from
\\{dvi\_file}, and \\{cur\_name} is a string variable that will be set to the
current font metric file name before \\{open\_tfm\_file} is called.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{cur\_loc}: \37\\{integer};\C{where we are about to look, in \\{dvi\_
file}}\6
\4\\{cur\_name}: \37\&{packed} \37\&{array} $[1\to\\{name\_length}]$ \1\&{of}\5
\\{char};\C{external name, 	with no lower case letters}\2\par

\M22. It turns out to be convenient to read four bytes at a time, when we are
inputting from \.{TFM} files. The input goes into global variables
\\{b0}, \\{b1}, \\{b2}, and \\{b3}, with \\{b0} getting the first byte and \\
{b3}
the fourth.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4$\\{b0},\39\\{b1},\39\\{b2},\39\\{b3}$: \37\\{eight\_bits};\C{four bytes
input at once}\par

\M23. The \\{read\_tfm\_word} procedure sets \\{b0} through \\{b3} to the next
four bytes in the current \.{TFM} file.

\Y\P\4\&{procedure}\1\  \37\\{read\_tfm\_word};\2\6
\&{begin} \37$\\{read}(\\{tfm\_file},\39\\{b0})$;\5
$\\{read}(\\{tfm\_file},\39\\{b1})$;\5
$\\{read}(\\{tfm\_file},\39\\{b2})$;\5
$\\{read}(\\{tfm\_file},\39\\{b3})$;\6
\&{end};\par

\M24. We shall use another set of simple functions to read the next byte or
bytes from \\{dvi\_file}. There are seven possibilities, each of which is
treated as a separate function in order to minimize the overhead for
subroutine calls.

\Y\P\4\&{function}\1\  \37\\{get\_byte}: \37\\{integer};\C{returns the next
byte, unsigned}\6
\4\&{var} \37\|b: \37\\{eight\_bits};\2\6
\&{begin} \37\&{if} $\\{eof}(\\{dvi\_file})$ \1\&{then}\5
$\\{get\_byte}\K0$\6
\4\&{else} \&{begin} \37$\\{read}(\\{dvi\_file},\39\|b)$;\5
$\\{incr}(\\{cur\_loc})$;\5
$\\{get\_byte}\K\|b$;\6
\&{end};\2\6
\&{end};\7
\4\&{function}\1\  \37\\{signed\_byte}: \37\\{integer};\C{returns the next
byte, signed}\6
\4\&{var} \37\|b: \37\\{eight\_bits};\2\6
\&{begin} \37$\\{read}(\\{dvi\_file},\39\|b)$;\5
$\\{incr}(\\{cur\_loc})$;\6
\&{if} $\|b<128$ \1\&{then}\5
$\\{signed\_byte}\K\|b$\ \&{else} $\\{signed\_byte}\K\|b-256$;\2\6
\&{end};\7
\4\&{function}\1\  \37\\{get\_two\_bytes}: \37\\{integer};\C{returns the next
two bytes, unsigned}\6
\4\&{var} \37$\|a,\39\|b$: \37\\{eight\_bits};\2\6
\&{begin} \37$\\{read}(\\{dvi\_file},\39\|a)$;\5
$\\{read}(\\{dvi\_file},\39\|b)$;\5
$\\{cur\_loc}\K\\{cur\_loc}+2$;\5
$\\{get\_two\_bytes}\K\|a\ast256+\|b$;\6
\&{end};\7
\4\&{function}\1\  \37\\{signed\_pair}: \37\\{integer};\C{returns the next two
bytes, signed}\6
\4\&{var} \37$\|a,\39\|b$: \37\\{eight\_bits};\2\6
\&{begin} \37$\\{read}(\\{dvi\_file},\39\|a)$;\5
$\\{read}(\\{dvi\_file},\39\|b)$;\5
$\\{cur\_loc}\K\\{cur\_loc}+2$;\6
\&{if} $\|a<128$ \1\&{then}\5
$\\{signed\_pair}\K\|a\ast256+\|b$\6
\4\&{else} $\\{signed\_pair}\K(\|a-256)\ast256+\|b$;\2\6
\&{end};\7
\4\&{function}\1\  \37\\{get\_three\_bytes}: \37\\{integer};\C{returns the next
three bytes, unsigned}\6
\4\&{var} \37$\|a,\39\|b,\39\|c$: \37\\{eight\_bits};\2\6
\&{begin} \37$\\{read}(\\{dvi\_file},\39\|a)$;\5
$\\{read}(\\{dvi\_file},\39\|b)$;\5
$\\{read}(\\{dvi\_file},\39\|c)$;\5
$\\{cur\_loc}\K\\{cur\_loc}+3$;\5
$\\{get\_three\_bytes}\K(\|a\ast256+\|b)\ast256+\|c$;\6
\&{end};\7
\4\&{function}\1\  \37\\{signed\_trio}: \37\\{integer};\C{returns the next
three bytes, signed}\6
\4\&{var} \37$\|a,\39\|b,\39\|c$: \37\\{eight\_bits};\2\6
\&{begin} \37$\\{read}(\\{dvi\_file},\39\|a)$;\5
$\\{read}(\\{dvi\_file},\39\|b)$;\5
$\\{read}(\\{dvi\_file},\39\|c)$;\5
$\\{cur\_loc}\K\\{cur\_loc}+3$;\6
\&{if} $\|a<128$ \1\&{then}\5
$\\{signed\_trio}\K(\|a\ast256+\|b)\ast256+\|c$\6
\4\&{else} $\\{signed\_trio}\K((\|a-256)\ast256+\|b)\ast256+\|c$;\2\6
\&{end};\7
\4\&{function}\1\  \37\\{signed\_quad}: \37\\{integer};\C{returns the next four
bytes, signed}\6
\4\&{var} \37$\|a,\39\|b,\39\|c,\39\|d$: \37\\{eight\_bits};\2\6
\&{begin} \37$\\{read}(\\{dvi\_file},\39\|a)$;\5
$\\{read}(\\{dvi\_file},\39\|b)$;\5
$\\{read}(\\{dvi\_file},\39\|c)$;\5
$\\{read}(\\{dvi\_file},\39\|d)$;\5
$\\{cur\_loc}\K\\{cur\_loc}+4$;\6
\&{if} $\|a<128$ \1\&{then}\5
$\\{signed\_quad}\K((\|a\ast256+\|b)\ast256+\|c)\ast256+\|d$\6
\4\&{else} $\\{signed\_quad}\K(((\|a-256)\ast256+\|b)\ast256+\|c)\ast256+\|d$;\2
\6
\&{end};\par

\M25. Finally we come to the routines that are used only if \\{random\_reading}
is
\\{true}. The driver program below needs two such routines: \\{dvi\_length}
should
compute the total number of bytes in \\{dvi\_file}, possibly also
causing $\\{eof}(\\{dvi\_file})$ to be true; and $\\{move\_to\_byte}(\|n)$
should position \\{dvi\_file} so that the next \\{get\_byte} will read byte \|
n,
starting with $\|n=0$ for the first byte in the file.

Such routines are, of course, highly system dependent. They are implemented
here in terms of two assumed system routines called \\{set\_pos} and \\{cur\_
pos}.
The call $\\{set\_pos}(\|f,\|n)$ moves to item \|n in file \|f, unless \|n is
negative or larger than the total number of items in \|f; in the latter
case, $\\{set\_pos}(\|f,\|n)$ moves to the end of file \|f.
The call $\\{cur\_pos}(\|f)$ gives the total number of items in \|f, if
$\\{eof}(\|f)$ is true; we use \\{cur\_pos} only in such a situation.

\Y\P\4\&{function}\1\  \37\\{dvi\_length}: \37\\{integer};\2\6
\&{begin} \37$\\{set\_pos}(\\{dvi\_file},\39-1)$;\5
$\\{dvi\_length}\K\\{cur\_pos}(\\{dvi\_file})$;\6
\&{end};\7
\4\&{procedure}\1\  \37$\\{move\_to\_byte}(\|n:\\{integer})$;\2\6
\&{begin} \37$\\{set\_pos}(\\{dvi\_file},\39\|n)$;\5
$\\{cur\_loc}\K\|n$;\6
\&{end};\par

\N26.  Reading the font information.
\.{DVI} file format does not include information about character widths, since
that would tend to make the files a lot longer. But a program that reads
a \.{DVI} file is supposed to know the widths of the characters that appear
in \\{set\_char} commands. Therefore \.{DVItype} looks at the font metric
(\.{TFM}) files for the fonts that are involved.

The character-width data appears also in other files (e.g., in \.{PXL} files
that contain bit patterns for printing characters on low-resolution devices);
thus, it is usually possible for \.{DVI} reading programs to get by with
accessing only one file per font. \.{DVItype} has a comparatively easy
task in this regard, since it needs only a few words of information from
each font; other \.{DVI}-to-printer programs may have to go to some pains to
deal with complications that arise when a large number of large font files
all need to be accessed simultaneously.


\M27. For purposes of this program, we need to know only two things about a
given character \|c in a given font \|f: (1)@Is \|c a legal character
in@\|f? (2)@If so, what is the width of \|c? We also need to know the
symbolic name of each font, so it can be printed out, and we need to know
the approximate size of inter-word spaces in each font.

The answers to these questions appear implicitly in the following data
structures. The current number of known fonts is \\{nf}. Each known font has
an internal number \|f, where $0\L\|f<\\{nf}$; the external number of this
font,
i.e., its font identification number in the \.{DVI} file, is
$\\{font\_num}[\|f]$, and the external name of this font is the string that
occupies positions $\\{font\_name}[\|f]$ through $\\{font\_name}[\|f+1]-1$ of
the array
\\{names}. The latter array consists of \\{ascii\_code} characters, and
$\\{font\_name}[\\{nf}]$ is its first unoccupied position.  A horizontal motion
less than $\\{font\_space}[\|f]$ will be treated as a `kern' that is not
indicated in the printouts that \.{DVItype} produces between brackets. The
legal characters run from $\\{font\_bc}[\|f]$ to $\\{font\_ec}[\|f]$,
inclusive; more
precisely, a given character \|c is valid in font \|f if and only if
$\\{font\_bc}[\|f]\L\|c\L\\{font\_ec}[\|f]$ and $\\{char\_width}(\|f)(\|c)\I\\
{invalid\_width}$.
(Exception: If $\\{font\_ec}[\|f]=256$, all characters $\|c\G256$ are valid and
have
the same width $\\{char\_width}(\|f)(256)$.)
Finally, $\\{char\_width}(\|f)(\|c)=\\{width}[\\{width\_base}[\|f]+\|c]$, and \\
{width\_ptr} is the
first unused position of the \\{width} array.

\Y\P\D \37$\\{char\_width\_end}(\#)\S\#$ ] \par
\P\D $\\{char\_width}(\#)\S\\{width}$ [ $\\{width\_base}[\#]+\\{char\_width\_
end}$\par
\P\D \37$\\{invalid\_width}\S\O17777777777$\par
\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{font\_num}: \37\&{array} $[0\to\\{max\_fonts}]$ \1\&{of}\5
\\{integer};\C{external font numbers}\2\6
\4\\{font\_name}: \37\&{array} $[0\to\\{max\_fonts}]$ \1\&{of}\5
$0\to\\{name\_size}$;\C{starting positions 	of external font names}\2\6
\4\\{names}: \37\&{array} $[0\to\\{name\_size}]$ \1\&{of}\5
\\{ascii\_code};\C{characters of names}\2\6
\4\\{font\_space}: \37\&{array} $[0\to\\{max\_fonts}]$ \1\&{of}\5
\\{integer};\C{boundary between ``small'' 	and ``large'' spaces}\2\6
\4\\{font\_bc}: \37\&{array} $[0\to\\{max\_fonts}]$ \1\&{of}\5
\\{integer};\C{beginning characters in fonts}\2\6
\4\\{font\_ec}: \37\&{array} $[0\to\\{max\_fonts}]$ \1\&{of}\5
\\{integer};\C{ending characters in fonts}\2\6
\4\\{width\_base}: \37\&{array} $[0\to\\{max\_fonts}]$ \1\&{of}\5
$0\to\\{max\_widths}$;\C{index into \\{width} table}\2\6
\4\\{width}: \37\&{array} $[0\to\\{max\_widths}]$ \1\&{of}\5
\\{integer};\C{character widths, in \.{DVI} units}\2\6
\4\\{nf}: \37$0\to\\{max\_fonts}$;\C{the number of known fonts}\6
\4\\{width\_ptr}: \37$0\to\\{max\_widths}$;\C{the number of known character
widths}\par

\M28. \P$\X10:Set initial values\X\mathrel{+}\S$\6
$\\{nf}\K0$;\5
$\\{width\_ptr}\K0$;\5
$\\{font\_name}[0]\K0$;\par

\M29. It is, of course, a simple matter to print the name of a given font.

\Y\P\4\&{procedure}\1\  \37$\\{print\_font}(\|f:\\{integer})$;\6
\4\&{var} \37\|k: \37$0\to\\{name\_size}$;\C{index into \\{names}}\2\6
\&{begin} \37\&{if} $\|f=\\{nf}$ \1\&{then}\5
$\\{print}(\.{\'undefined\ font!\'})$\6
\4\&{else} \&{begin} \37\&{for} $\|k\K\\{font\_name}[\|f]\mathrel{\&{to}}\\
{font\_name}[\|f+1]-1$ \1\&{do}\5
$\\{print}(\\{xchr}[\\{names}[\|k]])$;\2\6
\&{end};\2\6
\&{end};\par

\M30. An auxiliary array \\{in\_width} is used to hold the widths as they are
input. The global variable \\{tfm\_check\_sum} is set to the check sum that
appears in the current \.{TFM} file.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{in\_width}: \37\&{array} $[0:255]$ \1\&{of}\5
\\{integer};\C{\.{TFM} width data in \.{DVI} units}\2\6
\4\\{tfm\_check\_sum}: \37\\{integer};\C{check sum found in \\{tfm\_file}}\par

\M31. Here is a procedure that absorbs the necessary information from a
\.{TFM} file, assuming that the file has just been successfully reset
so that we are ready to read its first byte. (A complete description of
\.{TFM} file format appears in the documentation of \.{TFtoPL} and will
not be repeated here.) The procedure does not check the \.{TFM} file
for validity, nor does it give explicit information about what is
wrong with a \.{TFM} file that proves to be invalid; \.{DVI}-reading
programs need not do this, since \.{TFM} files are almost always valid,
and since the \.{TFtoPL} utility program has been specifically designed
to diagnose \.{TFM} errors. The procedure simply returns \\{false} if it
detects anything amiss in the \.{TFM} data.

There is a parameter, \|z, which represents the scaling factor being
used to compute the font dimensions; it must be in the range $0<z<2↑{27}$.

\Y\P\4\&{function}\1\  \37$\\{in\_TFM}(\|z:\\{integer})$: \37\\
{boolean};\C{input \.{TFM} data or return \\{false}}\6
\4\&{label} \37$9997,\39$\C{go here when the format is bad}\6
$9998,\39$\C{go here when the information cannot be loaded}\6
9999;\C{go here to exit}\6
\4\&{var} \37\|k: \37\\{integer};\C{index for loops}\6
\\{lh}: \37\\{integer};\C{length of the header data, in four-byte words}\6
\\{nw}: \37\\{integer};\C{number of words in the width table}\6
\\{wp}: \37$0\to\\{max\_widths}$;\C{new value of \\{width\_ptr} after
successful input}\6
$\\{alpha},\39\\{beta}$: \37\\{integer};\C{quantities used in the scaling
computation}\2\6
\&{begin} \37\X32:Read past the header data; \&{goto} 9997 if there is a
problem\X;\6
\X33:Store character-width indices at the end of the \\{width} table\X;\6
\X34:Read and convert the width values, setting up the \\{in\_width} table\X;\6
\X37:Move the widths from \\{in\_width} to \\{width}, and append \\{pixel\_
width} values\X;\6
$\\{width\_ptr}\K\\{wp}$;\5
$\\{in\_TFM}\K\\{true}$;\5
\&{goto} \379999;\6
\49997: \37$\\{print\_ln}(\.{\'---not\ loaded,\ TFM\ file\ is\ bad\'})$;\6
\49998: \37$\\{in\_TFM}\K\\{false}$;\6
\49999: \37\&{end};\par

\M32. \P$\X32:Read past the header data; \&{goto} 9997 if there is a
problem\X\S$\6
\\{read\_tfm\_word};\5
$\\{lh}\K\\{b2}\ast256+\\{b3}$;\5
\\{read\_tfm\_word};\5
$\\{font\_bc}[\\{nf}]\K\\{b0}\ast256+\\{b1}$;\5
$\\{font\_ec}[\\{nf}]\K\\{b2}\ast256+\\{b3}$;\6
\&{if} $\\{font\_ec}[\\{nf}]<\\{font\_bc}[\\{nf}]$ \1\&{then}\5
$\\{font\_bc}[\\{nf}]\K\\{font\_ec}[\\{nf}]+1$;\2\6
\&{if} $\\{width\_ptr}+\\{font\_ec}[\\{nf}]-\\{font\_bc}[\\{nf}]+1>\\{max\_
widths}$ \1\&{then}\6
\&{begin} \37$\\{print\_ln}(\.{\'---not\ loaded,\ DVItype\ needs\ larger\
width\ table\'})$;\5
\&{goto} \379998;\6
\&{end};\2\6
$\\{wp}\K\\{width\_ptr}+\\{font\_ec}[\\{nf}]-\\{font\_bc}[\\{nf}]+1$;\5
\\{read\_tfm\_word};\5
$\\{nw}\K\\{b0}\ast256+\\{b1}$;\6
\&{if} $(\\{nw}=0)\V(\\{nw}>256)$ \1\&{then}\5
\&{goto} \379997;\2\6
\&{for} $\|k\K1\mathrel{\&{to}}3+\\{lh}$ \1\&{do}\6
\&{begin} \37\&{if} $\\{eof}(\\{tfm\_file})$ \1\&{then}\5
\&{goto} \379997;\2\6
\\{read\_tfm\_word};\6
\&{if} $\|k=4$ \1\&{then}\6
\&{if} $\\{b0}<128$ \1\&{then}\5
$\\{tfm\_check\_sum}\K((\\{b0}\ast256+\\{b1})\ast256+\\{b2})\ast256+\\{b3}$\6
\4\&{else} $\\{tfm\_check\_sum}\K(((\\{b0}-256)\ast256+\\{b1})\ast256+\\
{b2})\ast256+\\{b3}$;\2\2\6
\&{end};\2\par
\U section 31.

\M33. \P$\X33:Store character-width indices at the end of the \\{width}
table\X\S$\6
\&{if} $\\{wp}>0$ \1\&{then}\6
\&{for} $\|k\K\\{width\_ptr}\mathrel{\&{to}}\\{wp}-1$ \1\&{do}\6
\&{begin} \37\\{read\_tfm\_word};\6
\&{if} $\\{b0}>\\{nw}$ \1\&{then}\5
\&{goto} \379997;\2\6
$\\{width}[\|k]\K\\{b0}$;\6
\&{end};\2\2\par
\U section 31.

\M34. The most important part of \\{in\_TFM} is the width computation, which
involves multiplying the relative widths in the \.{TFM} file by the
scaling factor in the \.{DVI} file. This fixed-point multiplication
must be done with precisely the same accuracy by all \.{DVI}-reading programs,
in order to validate the assumptions made by \.{DVI}-writing programs
like \TEX82. 

Let us therefore summarize what needs to be done. Each width in a \.{TFM}
file appears as a four-byte quantity called a \\{fix\_word}.  A \\{fix\_word}
whose respective bytes are $(a,b,c,d)$ represents the number
$$x=\left\{\vcenter{\halign{\lft{$#$,}\qquad&if \lft{$#$}\cr
b\cdot2↑{-4}+c\cdot2↑{-12}+d\cdot2↑{-20}&a=0;\cr
-16+b\cdot2↑{-4}+c\cdot2↑{-12}+d\cdot2↑{-20}&a=255.\cr}}\right.$$
(No other choices of $a$ are allowed, since the magnitude of a \.{TFM}
dimension must be less than 16.)  We want to multiply this quantity by the
integer@\|z, which is known to be less then $2↑{27}$. Let $\alpha=16z$.
If $\|z<2↑{23}$, the individual multiplications $b\cdot z$, $c\cdot z$,
$d\cdot z$ cannot overflow; otherwise we will divide \|z by 2, 4, 8, or
16, to obtain a multiplier less than $2↑{23}$, and we can compensate for
this later. If \|z has thereby been replaced by $\|z↑\prime=\|z/2↑e$, let
$\beta=2↑{4-e}$; we shall compute
$$\lfloor(b+c\cdot2↑{-8}+d\cdot2↑{-16})\,z↑\prime/\beta\rfloor$$ if $a=0$,
or the same quantity minus $\alpha$ if $a=255$.  This calculation must be
done exactly, for the reasons stated above; the following program does the
job in a system-independent way, assuming that arithmetic is exact on
numbers less than $2↑{31}$ in magnitude.

\Y\P$\4\X34:Read and convert the width values, setting up the \\{in\_width}
table\X\S$\6
\X35:Replace \|z by $\|z↑\prime$ and compute $\alpha,\beta$\X;\6
\&{for} $\|k\K0\mathrel{\&{to}}\\{nw}-1$ \1\&{do}\6
\&{begin} \37\\{read\_tfm\_word};\5
$\\{in\_width}[\|k]\K(((((\\{b3}\ast\|z)\mathbin{\&{div}}\O400)+(\\{b2}\ast\|
z))\mathbin{\&{div}}\O400)+(\\{b1}\ast\|z))\mathbin{\&{div}}\\{beta}$;\6
\&{if} $\\{b0}>0$ \1\&{then}\6
\&{if} $\\{b0}<255$ \1\&{then}\5
\&{goto} \379997\6
\4\&{else} $\\{in\_width}[\|k]\K\\{in\_width}[\|k]-\\{alpha}$;\2\2\6
\&{end}\2\par
\U section 31.

\M35. \P$\X35:Replace \|z by $\|z↑\prime$ and compute $\alpha,\beta$\X\S$\6
\&{begin} \37$\\{alpha}\K16\ast\|z$;\5
$\\{beta}\K16$;\6
\&{while} $\|z\G\O40000000$ \1\&{do}\6
\&{begin} \37$\|z\K\|z\mathbin{\&{div}}2$;\5
$\\{beta}\K\\{beta}\mathbin{\&{div}}2$;\6
\&{end};\2\6
\&{end}\par
\U section 34.

\M36. A \.{DVI}-reading program usually works with font files instead of
\.{TFM} files, so \.{DVItype} is atypical in that respect. Font files
should, however, contain exactly the same character width data that is
found in the corresponding \.{TFM}s. In addition, font files usually
also contain the widths of characters in pixels, since the device-independent
character widths of \.{TFM} files are generally not perfect multiples of
pixels.

The \\{pixel\_width} array contains this information; when $\\{width}[\|k]$ is
the
device-independent width of some character in \.{DVI} units, $\\{pixel\_
width}[\|k]$
is the corresponding width of that character in an actual font.
The macro \\{char\_pixel\_width} is set up to be analogous to \\{char\_width}.

\Y\P\D $\\{char\_pixel\_width}(\#)\S\\{pixel\_width}$ [ $\\{width\_base}[\#]+\\
{char\_width\_end}$\par
\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{pixel\_width}: \37\&{array} $[0\to\\{max\_widths}]$ \1\&{of}\5
\\{integer};\C{actual character widths, 	in pixels}\2\6
\4\\{conv}: \37\\{real};\C{converts \.{DVI} units to pixels}\par

\M37. The following code computes pixel widths by simply rounding the \.{TFM}
widths to the nearest integer number of pixels, based on the conversion factor
\\{conv} that converts \.{DVI} units to pixels. However, such a simple
formula will not be valid for all fonts, and it will often give results that
are off by \hbox{$\pm1$} when a low-resolution font has been carefully
hand-fitted. For example, a font designer often wants to make the letter `m'
a pixel wider or narrower in order to make the font appear more consistent.
\.{DVI}-to-printer programs should therefore input the correct pixel width
information from font files whenever there is a chance that it may differ.
A warning message may also be desirable in the case that at least one character
is found whose pixel width differs from $\\{conv}\ast\\{width}$ by more than a
full pixel.

\Y\P\D \37$\\{pixel\_round}(\#)\S\\{trunc}(\\{conv}\ast(\#)+0.5)$\par
\Y\P$\4\X37:Move the widths from \\{in\_width} to \\{width}, and append \\
{pixel\_width} values\X\S$\6
$\\{width\_base}[\\{nf}]\K\\{width\_ptr}-\\{font\_bc}[\\{nf}]$;\6
\&{if} $\\{wp}>0$ \1\&{then}\6
\&{for} $\|k\K\\{width\_ptr}\mathrel{\&{to}}\\{wp}-1$ \1\&{do}\6
\&{begin} \37$\\{width}[\|k]\K\\{in\_width}[\\{width}[\|k]]$;\5
$\\{pixel\_width}[\|k]\K\\{pixel\_round}(\\{width}[\|k])$;\6
\&{end}\2\2\par
\U section 31.

\N38.  Optional modes of output.
\.{DVItype} will print different quantities of information based on some
options that the user can specify: The \\{out\_mode} level is set to one of
three values (\\{errors\_only}, \\{terse}, \\{verbose}), giving different
degrees
of output; and the typeout can be confined to a restricted subset of the
pages by specifying the desired starting page and the maximum number
of pages. Furthermore there is an option to specify the resolution of an
assumed discrete output device, so that pixel-oriented calculations will
be shown; and there is an option to override the magnification factor
that is stated in the \.{DVI} file.

The starting page is specified by giving a sequence of 1 to 10 numbers or
asterisks separated by dots. For example, the specification `\.{1.*.-5}'
can be used to refer to a page output by \TEX\ when $\.{\\count0}=1$
and $\.{\\count2}=-5$. (Recall that \\{bop} commands in a \.{DVI} file
are followed by ten `count' values.) An asterisk matches any number,
so the `\.*' in `\.{1.*.-5}' means that \.{\\count1} is ignored when
specifying the first page. If several pages match the given specification,
\.{DVItype} will begin with the earliest such page in the file. The
default specification `\.*' (which matches all pages) therefore denotes
the page at the beginning of the file.

When \.{DVItype} begins, it engages the user in a brief dialog so that the
options can be specified. This part of \.{DVItype} requires nonstandard
\PASCAL\ constructions to handle the online interaction; so it may be
preferable in some cases to omit the dialog and simply to stick to the
default options ($\\{out\_mode}=\\{verbose}$, starting page `\.*',
$\\{max\_pages}=1000000$, $\\{resolution}=240.0$, $\\{new\_mag}=0$).  On other
hand, the
system-dependent routines that are needed are not complicated, so it will
not be terribly difficult to introduce them.

\Y\P\D \37$\\{errors\_only}=0$\C{value of \\{out\_mode} when minimal printing
occurs}\par
\P\D \37$\\{terse}=1$\C{value of \\{out\_mode} for abbreviated output}\par
\P\D \37$\\{verbose}=2$\C{value of \\{out\_mode} when you want the works}\par
\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{out\_mode}: \37$\\{errors\_only}\to\\{verbose}$;\C{controls the amount of
output}\6
\4\\{max\_pages}: \37\\{integer};\C{at most this many $\\{bop}\to\\{eop}$ pages
will be printed}\6
\4\\{resolution}: \37\\{real};\C{pixels per inch}\6
\4\\{new\_mag}: \37\\{integer};\C{if positive, overrides the postamble's
magnification}\par

\M39. The starting page specification is recorded in two arrays called
\\{start\_count} and \\{start\_there}. For example, `\.{1.*.-5}' is represented
by $\\{start\_there}[0]=\\{true}$, $\\{start\_count}[0]=1$, $\\{start\_
there}[1]=\\{false}$,
$\\{start\_there}[2]=\\{true}$, $\\{start\_count}[2]=-5$.
We also set $\\{start\_vals}=2$, to indicate that count 2 was the last one
mentioned. The other values of \\{start\_count} and \\{start\_there} are not
important.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{start\_count}: \37\&{array} $[0\to9]$ \1\&{of}\5
\\{integer};\C{count values to select starting page}\2\6
\4\\{start\_there}: \37\&{array} $[0\to9]$ \1\&{of}\5
\\{boolean};\C{is the \\{start\_count} value relevant?}\2\6
\4\\{start\_vals}: \37$0\to9$;\C{the last count considered significant}\6
\4\\{count}: \37\&{array} $[0\to9]$ \1\&{of}\5
\\{integer};\C{the count values on the current page}\2\par

\M40. \P$\X10:Set initial values\X\mathrel{+}\S$\6
$\\{out\_mode}\K\\{verbose}$;\5
$\\{max\_pages}\K1000000$;\5
$\\{start\_vals}\K0$;\5
$\\{start\_there}[0]\K\\{false}$;\par

\M41. Here is a simple subroutine that tests if the current page might be the
starting page.

\Y\P\4\&{function}\1\  \37\\{start\_match}: \37\\{boolean};\C{does \\{count}
match the starting spec?}\6
\4\&{var} \37\|k: \37$0\to9$;\C{loop index}\6
\\{match}: \37\\{boolean};\C{does everything match so far?}\2\6
\&{begin} \37$\\{match}\K\\{true}$;\6
\&{for} $\|k\K0\mathrel{\&{to}}\\{start\_vals}$ \1\&{do}\6
\&{if} $\\{start\_there}[\|k]\W(\\{start\_count}[\|k]\I\\{count}[\|k])$ \1\&
{then}\5
$\\{match}\K\\{false}$;\2\2\6
$\\{start\_match}\K\\{match}$;\6
\&{end};\par

\M42. The \\{input\_ln} routine waits for the user to type a line at his or her
terminal; then it puts ascii-code equivalents for the characters on that line
into the \\{buffer} array. The \\{term\_in} file is used for terminal input,
and \\{term\_out} for terminal output.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{buffer}: \37\&{array} $[0\to\\{terminal\_line\_length}]$ \1\&{of}\5
\\{ascii\_code};\2\6
\4\\{term\_in}: \37\\{text\_file};\C{the terminal, considered as an input
file}\6
\4\\{term\_out}: \37\\{text\_file};\C{the terminal, considered as an output
file}\par

\M43. Since the terminal is being used for both input and output, some systems
need a special routine to make sure that the user can see a prompt message
before waiting for input based on that message. (Otherwise the message
may just be sitting in a hidden buffer somewhere, and the user will have
no idea what the program is waiting for.) We shall call a system-dependent
subroutine \\{update\_terminal} in order to avoid this problem.

\Y\P\D \37$\\{update\_terminal}\S\\{break}(\\{term\_out})$\C{empty the terminal
output buffer}\par

\M44. During the dialog, \.{DVItype} will treat the first blank space in a
line as the end of that line. Therefore \\{input\_ln} makes sure that there
is always at least one blank space in \\{buffer}.

\Y\P\4\&{procedure}\1\  \37\\{input\_ln};\C{inputs a line from the terminal}\6
\4\&{var} \37\|k: \37$0\to\\{terminal\_line\_length}$;\2\6
\&{begin} \37\\{update\_terminal};\5
$\\{reset}(\\{term\_in})$;\6
\&{if} $\\{eoln}(\\{term\_in})$ \1\&{then}\5
$\\{read\_ln}(\\{term\_in})$;\2\6
$\|k\K0$;\6
\&{while} $(\|k<\\{terminal\_line\_length})\W\R\\{eoln}(\\{term\_in})$ \1\&
{do}\6
\&{begin} \37$\\{buffer}[\|k]\K\\{xord}[\\{term\_in}\↑]$;\5
$\\{incr}(\|k)$;\5
$\\{get}(\\{term\_in})$;\6
\&{end};\2\6
$\\{buffer}[\|k]\K\.{"\ "}$;\6
\&{end};\par

\M45. The global variable \\{buf\_ptr} is used while scanning each line of
input;
it points to the first unread character in \\{buffer}.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{buf\_ptr}: \37$0\to\\{terminal\_line\_length}$;\C{the number of characters
read}\par

\M46. Here is a routine that scans a (possibly signed) integer and computes
the decimal value. If no decimal integer starts at \\{buf\_ptr}, the
value 0 is returned. The integer should be less than $2↑{31}$ in
absolute value.

\Y\P\4\&{function}\1\  \37\\{get\_integer}: \37\\{integer};\6
\4\&{var} \37\|x: \37\\{integer};\C{accumulates the value}\6
\\{negative}: \37\\{boolean};\C{should the value be negated?}\2\6
\&{begin} \37\&{if} $\\{buffer}[\\{buf\_ptr}]=\.{"-"}$ \1\&{then}\6
\&{begin} \37$\\{negative}\K\\{true}$;\5
$\\{incr}(\\{buf\_ptr})$;\6
\&{end}\6
\4\&{else} $\\{negative}\K\\{false}$;\2\6
$\|x\K0$;\6
\&{while} $(\\{buffer}[\\{buf\_ptr}]\G\.{"0"})\W(\\{buffer}[\\{buf\_ptr}]\L\.
{"9"})$ \1\&{do}\6
\&{begin} \37$\|x\K10\ast\|x+\\{buffer}[\\{buf\_ptr}]-\.{"0"}$;\5
$\\{incr}(\\{buf\_ptr})$;\6
\&{end};\2\6
\&{if} $\\{negative}$ \1\&{then}\5
$\\{get\_integer}\K-\|x$\ \&{else} $\\{get\_integer}\K\|x$;\2\6
\&{end};\par

\M47. The selected options are put into global variables by the \\{dialog}
procedure, which is called just as \.{DVItype} begins.

\Y\P\4\&{procedure}\1\  \37\\{dialog};\6
\4\&{label} \37$1,\392,\393,\394,\395$;\6
\4\&{var} \37\|k: \37\\{integer};\C{loop variable}\2\6
\&{begin} \37$\\{rewrite}(\\{term\_out})$;\C{prepare the terminal for output}\6
\X48:Determine the desired \\{out\_mode}\X;\6
\X49:Determine the desired \\{start\_count} values\X;\6
\X50:Determine the desired \\{max\_pages}\X;\6
\X51:Determine the desired \\{resolution}\X;\6
\X52:Determine the desired \\{new\_mag}\X;\6
\X53:Print all the selected options\X;\6
\&{end};\par

\M48. \P$\X48:Determine the desired \\{out\_mode}\X\S$\6
\41: \37$\\{write}(\\{term\_out},\39\.{\'Output\ level\ (default=2,\ ?\ for\
help):\ \'})$;\5
$\\{out\_mode}\K2$;\5
\\{input\_ln};\6
\&{if} $\\{buffer}[0]\I\.{"\ "}$ \1\&{then}\6
\&{if} $(\\{buffer}[0]\G\.{"0"})\W(\\{buffer}[0]\L\.{"2"})$ \1\&{then}\5
$\\{out\_mode}\K\\{buffer}[0]-\.{"0"}$\6
\4\&{else} \&{begin} \37$\\{write}(\\{term\_out},\39\.{\'Type\ 2\ for\
complete\ listing,\'})$;\5
$\\{write}(\\{term\_out},\39\.{\'\ 0\ for\ errors\ only,\'})$;\5
$\\{write\_ln}(\\{term\_out},\39\.{\'\ 1\ for\ something\ in\ between.\'})$;\5
\&{goto} \371;\6
\&{end}\2\2\par
\U section 47.

\M49. \P$\X49:Determine the desired \\{start\_count} values\X\S$\6
\42: \37$\\{write}(\\{term\_out},\39\.{\'Starting\ page\ (default=*):\ \'})$;\5
$\\{start\_vals}\K0$;\5
$\\{start\_there}[0]\K\\{false}$;\5
\\{input\_ln};\5
$\\{buf\_ptr}\K0$;\5
$\|k\K0$;\6
\&{if} $\\{buffer}[0]\I\.{"\ "}$ \1\&{then}\6
\1\&{repeat} \37\&{if} $\\{buffer}[\\{buf\_ptr}]=\.{"*"}$ \1\&{then}\6
\&{begin} \37$\\{start\_there}[\|k]\K\\{false}$;\5
$\\{incr}(\\{buf\_ptr})$;\6
\&{end}\6
\4\&{else} \&{begin} \37$\\{start\_there}[\|k]\K\\{true}$;\5
$\\{start\_count}[\|k]\K\\{get\_integer}$;\6
\&{end};\2\6
\&{if} $(\|k<9)\W(\\{buffer}[\\{buf\_ptr}]=\.{"."})$ \1\&{then}\6
\&{begin} \37$\\{incr}(\|k)$;\5
$\\{incr}(\\{buf\_ptr})$;\6
\&{end}\6
\4\&{else} \&{if} $\\{buffer}[\\{buf\_ptr}]=\.{"\ "}$ \1\&{then}\5
$\\{start\_vals}\K\|k$\6
\4\&{else} \&{begin} \37$\\{write}(\\{term\_out},\39\.{\'Type,\ e.g.,\ 1.*.-5\
to\ specify\ the\ \'})$;\5
$\\{write\_ln}(\\{term\_out},\39\.{\'first\ page\ with\ \\count0=1,\ \\
count2=-5.\'})$;\5
\&{goto} \372;\6
\&{end};\2\2\6
\4\&{until}\5
$\\{start\_vals}=\|k$\2\2\par
\U section 47.

\M50. \P$\X50:Determine the desired \\{max\_pages}\X\S$\6
\43: \37$\\{write}(\\{term\_out},\39\.{\'Maximum\ number\ of\ pages\
(default=1000000):\ \'})$;\5
$\\{max\_pages}\K1000000$;\5
\\{input\_ln};\5
$\\{buf\_ptr}\K0$;\6
\&{if} $\\{buffer}[0]\I\.{"\ "}$ \1\&{then}\6
\&{begin} \37$\\{max\_pages}\K\\{get\_integer}$;\6
\&{if} $\\{max\_pages}\L0$ \1\&{then}\6
\&{begin} \37$\\{write\_ln}(\\{term\_out},\39\.{\'Please\ type\ a\ positive\
number.\'})$;\5
\&{goto} \373;\6
\&{end};\2\6
\&{end}\2\par
\U section 47.

\M51. \P$\X51:Determine the desired \\{resolution}\X\S$\6
\44: \37$\\{write}(\\{term\_out},\39\.{\'Assumed\ device\ resolution\'})$;\5
$\\{write}(\\{term\_out},\39\.{\'\ in\ pixels\ per\ inch\ (default=240/1):\ \'
})$;\5
$\\{resolution}\K240.0$;\5
\\{input\_ln};\5
$\\{buf\_ptr}\K0$;\6
\&{if} $\\{buffer}[0]\I\.{"\ "}$ \1\&{then}\6
\&{begin} \37$\|k\K\\{get\_integer}$;\6
\&{if} $(\|k>0)\W(\\{buffer}[\\{buf\_ptr}]=\.{"/"})\W(\\{buffer}[\\{buf\_
ptr}+1]>\.{"0"})\W(\\{buffer}[\\{buf\_ptr}+1]\L\.{"9"})$ \1\&{then}\6
\&{begin} \37$\\{incr}(\\{buf\_ptr})$;\5
$\\{resolution}\K\|k/\\{get\_integer}$;\6
\&{end}\6
\4\&{else} \&{begin} \37$\\{write}(\\{term\_out},\39\.{\'Type\ a\ ratio\ of\
positive\ integers;\'})$;\5
$\\{write\_ln}(\\{term\_out},\39\.{\'\ (1\ pixel\ per\ mm\ would\ be\ 254/10).\'
})$;\5
\&{goto} \374;\6
\&{end};\2\6
\&{end}\2\par
\U section 47.

\M52. \P$\X52:Determine the desired \\{new\_mag}\X\S$\6
\45: \37$\\{write}(\\{term\_out},\39\.{\'New\ magnification\ (default=0):\ \'
})$;\5
$\\{new\_mag}\K0$;\5
\\{input\_ln};\5
$\\{buf\_ptr}\K0$;\6
\&{if} $\\{buffer}[0]\I\.{"\ "}$ \1\&{then}\6
\&{if} $(\\{buffer}[0]\G\.{"0"})\W(\\{buffer}[0]\L\.{"9"})$ \1\&{then}\5
$\\{new\_mag}\K\\{get\_integer}$\6
\4\&{else} \&{begin} \37$\\{write}(\\{term\_out},\39\.{\'Type\ a\ positive\
integer\ to\ override\ \'})$;\5
$\\{write\_ln}(\\{term\_out},\39\.{\'the\ magnification\ in\ the\ DVI\ file.\'
})$;\5
\&{goto} \375;\6
\&{end}\2\2\par
\U section 47.

\M53. After the dialog is over, we print the options so that the user
can see what \.{DVItype} thought was specified.

\Y\P$\4\X53:Print all the selected options\X\S$\6
$\\{print\_ln}(\.{\'Options\ selected:\'})$;\5
$\\{print}(\.{\'\ \ Starting\ page\ =\ \'})$;\6
\&{for} $\|k\K0\mathrel{\&{to}}\\{start\_vals}$ \1\&{do}\6
\&{begin} \37\&{if} $\\{start\_there}[\|k]$ \1\&{then}\5
$\\{print}(\\{start\_count}[\|k]:0)$\6
\4\&{else} $\\{print}(\.{\'*\'})$;\2\6
\&{if} $\|k<\\{start\_vals}$ \1\&{then}\5
$\\{print}(\.{\'.\'})$\6
\4\&{else} $\\{print\_ln}(\.{\'\ \'})$;\2\6
\&{end};\2\6
$\\{print\_ln}(\.{\'\ \ Maximum\ number\ of\ pages\ =\ \'},\39\\{max\_
pages}:0)$;\5
$\\{print}(\.{\'\ \ Output\ level\ =\ \'},\39\\{out\_mode}:0)$;\6
\&{case} $\\{out\_mode}$ \1\&{of}\6
\4\\{errors\_only}: \37$\\{print\_ln}(\.{\'\ (showing\ bops\ and\ error\
messages\ only)\'})$;\6
\4\\{terse}: \37$\\{print\_ln}(\.{\'\ (terse)\'})$;\6
\4\\{verbose}: \37$\\{print\_ln}(\.{\'\ (verbose)\'})$;\2\6
\&{end};\6
$\\{print\_ln}(\.{\'\ \ Resolution\ =\ \'},\39\\{resolution}:12:8,\39\.{\'\
pixels\ per\ inch\'})$;\6
\&{if} $\\{new\_mag}>0$ \1\&{then}\5
$\\{print\_ln}(\.{\'\ \ New\ magnification\ factor\ =\ \'},\39\\{new\_
mag}/1000:8:3)$\2\par
\U section 47.

\N54.  Low level output routines.
Simple text in the \.{DVI} file is saved in a buffer until $\\{line\_length}-2$
characters have accumulated, or until some non-simple \.{DVI} operation
occurs. Then the accumulated text is printed on a line, surrounded by
brackets. The global variable \\{text\_ptr} keeps track of the number of
characters currently in the buffer.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{text\_ptr}: \37$0\to\\{line\_length}$;\C{the number of characters in \\
{text\_buf}}\6
\4\\{text\_buf}: \37\&{array} $[1\to\\{line\_length}]$ \1\&{of}\5
\\{ascii\_code};\C{saved characters}\2\par

\M55. \P$\X10:Set initial values\X\mathrel{+}\S$\6
$\\{text\_ptr}\K0$;\par

\M56. The \\{flush\_text} procedure will empty the buffer if there is something
in it.

\Y\P\4\&{procedure}\1\  \37\\{flush\_text};\6
\4\&{var} \37\|k: \37$0\to\\{line\_length}$;\C{index into \\{text\_buf}}\2\6
\&{begin} \37\&{if} $\\{text\_ptr}>0$ \1\&{then}\6
\&{begin} \37\&{if} $\\{out\_mode}>\\{errors\_only}$ \1\&{then}\6
\&{begin} \37$\\{print}(\.{\'[\'})$;\6
\&{for} $\|k\K1\mathrel{\&{to}}\\{text\_ptr}$ \1\&{do}\5
$\\{print}(\\{xchr}[\\{text\_buf}[\|k]])$;\2\6
$\\{print\_ln}(\.{\']\'})$;\6
\&{end};\2\6
$\\{text\_ptr}\K0$;\6
\&{end};\2\6
\&{end};\par

\M57. And the \\{out\_text} procedure puts something in it.

\Y\P\4\&{procedure}\1\  \37$\\{out\_text}(\|c:\\{ascii\_code})$;\2\6
\&{begin} \37\&{if} $\\{text\_ptr}=\\{line\_length}-2$ \1\&{then}\5
\\{flush\_text};\2\6
$\\{incr}(\\{text\_ptr})$;\5
$\\{text\_buf}[\\{text\_ptr}]\K\|c$;\6
\&{end};\par

\N58.  Translation to symbolic form.
The main work of \.{DVItype} is accomplished by the \\{do\_page} procedure,
which produces the output for an entire page, assuming that the \\{bop}
command for that page has already been processed. This procedure is
essentially an interpretive routine that reads and acts on the \.{DVI}
commands. 


\M59. The definition of \.{DVI} files refers to six registers,
$(h,v,w,x,y,z)$, which hold integer values in \.{DVI} units.  In practice,
we also need registers \\{hh} and \\{vv}, the pixel analogs of $h$ and $v$,
since it is not always true that $\\{hh}=\\{pixel\_round}(\|h)$ or
$\\{vv}=\\{pixel\_round}(\|v)$.

The stack of $(h,v,w,x,y,z)$ values is represented by eight arrays
called \\{hstack}, $\ldotss$, \\{zstack}, \\{hhstack}, and \\{vvstack}.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4$\|h,\39\|v,\39\|w,\39\|x,\39\|y,\39\|z,\39\\{hh},\39\\{vv}$: \37\\
{integer};\C{current state values}\6
\4$\\{hstack},\39\\{vstack},\39\\{wstack},\39\\{xstack},\39\\{ystack},\39\\
{zstack}$: \37\&{array} $[0\to\\{stack\_size}]$ \1\&{of}\5
\\{integer};\C{pushed down values in \.{DVI} units}\2\6
\4$\\{hhstack},\39\\{vvstack}$: \37\&{array} $[0\to\\{stack\_size}]$ \1\&{of}\5
\\{integer};\C{pushed down values in pixels}\2\par

\M60. Three characteristics of the pages (their \\{max\_v}, \\{max\_h}, and
\\{max\_stack\_depth}) are specified in the postamble, and a warning message
is printed if these limits are exceeded. Actually \\{max\_v} is set to
the maximum height plus depth of a page, and \\{max\_h} to the maximum width,
for purposes of page layout. Since characters can legally be set outside
of the page boundaries, it is not an error when \\{max\_v} or \\{max\_h} is
exceeded. But \\{max\_stack\_depth} should not be exceeded.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{max\_v}: \37\\{integer};\C{the value of $\\{abs}(\|v)$ should probably not
exceed this}\6
\4\\{max\_h}: \37\\{integer};\C{the value of $\\{abs}(\|h)$ should probably not
exceed this}\6
\4\\{max\_stack\_depth}: \37\\{integer};\C{the stack depth should not exceed
this}\par

\M61. Before we get into the details of \\{do\_page}, it is convenient to
consider a simpler routine that computes the first parameter of each
opcode.

\Y\P\D \37$\\{four\_cases}(\#)\S\#,\39\#+1,\39\#+2,\39\#+3$\par
\P\D \37$\\{eight\_cases}(\#)\S\\{four\_cases}(\#),\39\\{four\_cases}(\#+4)$\par
\P\D \37$\\{sixteen\_cases}(\#)\S\\{eight\_cases}(\#),\39\\{eight\_cases}(\#
+8)$\par
\P\D \37$\\{thirty\_two\_cases}(\#)\S\\{sixteen\_cases}(\#),\39\\{sixteen\_
cases}(\#+16)$\par
\P\D \37$\\{sixty\_four\_cases}(\#)\S\\{thirty\_two\_cases}(\#),\39\\{thirty\_
two\_cases}(\#+32)$\par
\Y\P\4\&{function}\1\  \37$\\{first\_par}(\|o:\\{eight\_bits})$: \37\\
{integer};\2\6
\&{begin} \37\&{case} $\|o$ \1\&{of}\6
\4$\\{sixty\_four\_cases}(\\{set\_char\_0}),\39\\{sixty\_four\_cases}(\\{set\_
char\_0}+64)$: \37$\\{first\_par}\K\|o-\\{set\_char\_0}$;\6
\4$\\{set1},\39\\{put1},\39\\{fnt1},\39\\{xxx}$: \37$\\{first\_par}\K\\{get\_
byte}$;\6
\4$\\{set1}+1,\39\\{put1}+1,\39\\{fnt1}+1$: \37$\\{first\_par}\K\\{get\_two\_
bytes}$;\6
\4$\\{set1}+2,\39\\{put1}+2,\39\\{fnt1}+2$: \37$\\{first\_par}\K\\{get\_three\_
bytes}$;\6
\4$\\{right1},\39\\{w1},\39\\{x1},\39\\{down1},\39\\{y1},\39\\{z1}$: \37$\\
{first\_par}\K\\{signed\_byte}$;\6
\4$\\{right1}+1,\39\\{w1}+1,\39\\{x1}+1,\39\\{down1}+1,\39\\{y1}+1,\39\\
{z1}+1$: \37$\\{first\_par}\K\\{signed\_pair}$;\6
\4$\\{right1}+2,\39\\{w1}+2,\39\\{x1}+2,\39\\{down1}+2,\39\\{y1}+2,\39\\
{z1}+2$: \37$\\{first\_par}\K\\{signed\_trio}$;\6
\4$\\{set1}+3,\39\\{set\_rule},\39\\{put1}+3,\39\\{put\_rule},\39\\{right1}+3,\3
9\\{w1}+3,\39\\{x1}+3,\39\\{down1}+3,\39\\{y1}+3,\39\\{z1}+3,\39\\{fnt1}+3$: \3
7$\\{first\_par}\K\\{signed\_quad}$;\6
\4$\\{nop},\39\\{bop},\39\\{eop},\39\\{push},\39\\{pop},\39\\{pst},\39\\
{undefined\_commands}$: \37$\\{first\_par}\K0$;\6
\4\\{w0}: \37$\\{first\_par}\K\|w$;\6
\4\\{x0}: \37$\\{first\_par}\K\|x$;\6
\4\\{y0}: \37$\\{first\_par}\K\|y$;\6
\4\\{z0}: \37$\\{first\_par}\K\|z$;\6
\4$\\{sixty\_four\_cases}(\\{fnt\_num\_0})$: \37$\\{first\_par}\K\|o-\\{fnt\_
num\_0}$;\2\6
\&{end};\6
\&{end};\par

\M62. Here is another subroutine that we need: It computes the number of
pixels in the height or width of a rule. Characters and rules will line up
properly if the sizes are computed precisely as specified here.  (Since
\\{conv} is computed with some floating-point roundoff error, in a
machine-dependent way, format designers who are tailoring something for a
particular resolution should not plan their measurements to come out to an
exact integer number of pixels; they should compute things so that the
rule dimensions are a little less than an integer number of pixels, e.g.,
4.99 instead of 5.00.)

\Y\P\4\&{function}\1\  \37$\\{rule\_pixels}(\|x:\\{integer})$: \37\\
{integer};\C{computes $\lceil\\{conv}\cdot x\rceil$}\6
\4\&{var} \37\|n: \37\\{integer};\2\6
\&{begin} \37$\|n\K\\{trunc}(\\{conv}\ast\|x)$;\6
\&{if} $\|n<\\{conv}\ast\|x$ \1\&{then}\5
$\\{rule\_pixels}\K\|n+1$\ \&{else} $\\{rule\_pixels}\K\|n$;\2\6
\&{end};\par

\M63. Strictly speaking, the \\{do\_page} procedure is really a function with
side effects, not a `\&{procedure}'; it returns the value \\{false} if
\.{DVItype} should be aborted because of some unusual happening. The
subroutine is organized as a typical interpreter, with a multiway branch
on the command code followed by \&{goto}  statements leading to routines that
finish up the activities common to different commands.

\Y\P\D \37$\\{fin\_set}=41$\C{label for commands that set or put a
character}\par
\P\D \37$\\{fin\_rule}=42$\C{label for commands that set or put a rule}\par
\P\D \37$\\{move\_right}=43$\C{label for commands that change \|h}\par
\P\D \37$\\{move\_down}=44$\C{label for commands that change \|v}\par
\P\D \37$\\{show\_state}=45$\C{label for commands that change \|s}\par
\P\D \37$\\{change\_font}=46$\C{label for commands that change \\{cur\_
font}}\par
\P\D \37$\\{done}=30$\C{label for the end of a command}\par
\Y\P\4\&{function}\1\  \37\\{do\_page}: \37\\{boolean};\6
\4\&{label} \37$\\{fin\_set},\39\\{fin\_rule},\39\\{move\_right},\39\\{move\_
down},\39\\{show\_state},\39\\{change\_font},\39\\{done},\399998,\399999$;\6
\4\&{var} \37\|o: \37\\{eight\_bits};\C{operation code of the current command}\6
$\|p,\39\|q$: \37\\{integer};\C{parameters of the current command}\6
\|a: \37\\{integer};\C{byte number of the current command}\6
\|s: \37\\{integer};\C{current stack size}\6
\\{ss}: \37\\{integer};\C{stack size to print}\6
\\{cur\_font}: \37\\{integer};\C{current internal font number}\6
\|k: \37\\{integer};\C{loop index}\6
\\{showing}: \37\\{boolean};\C{is the current command being translated in
full?}\6
\\{bad\_char}: \37\\{boolean};\C{has a non-ascii character code appeared in
this \\{xxx}?}\2\6
\&{begin} \37$\\{cur\_font}\K\\{nf}$;\C{set current font undefined}\6
$\|s\K0$;\5
$\|h\K0$;\5
$\|v\K0$;\5
$\|w\K0$;\5
$\|x\K0$;\5
$\|y\K0$;\5
$\|z\K0$;\5
$\\{hh}\K0$;\5
$\\{vv}\K0$;\C{initialize the state variables}\6
\&{while} $\\{true}$ \1\&{do}\5
\X64:Translate the next command in the \.{DVI} file; \&{goto} 9999 with $\\{do\_
page}=\\{true}$ if it was \\{eop}; \&{goto} 9998 if premature termination is
needed\X;\2\6
\49998: \37$\\{print\_ln}(\.{\'!\'})$;\5
$\\{do\_page}\K\\{false}$;\6
\49999: \37\&{end};\par

\M64. Commands are broken down into ``major'' and ``minor'' categories:
A major command is always shown in full, while a minor one is
put into the buffer in abbreviated form. Minor commands, which
account for the bulk of most \.{DVI} files, involve horizontal spacing
and the typesetting of characters in a line; these are shown in full
only if $\\{out\_mode}=\\{verbose}$.

\Y\P\D \37$\\{show}(\#)\S$\1\6
\&{begin} \37\\{flush\_text};\5
$\\{showing}\K\\{true}$;\5
$\\{print}(\|a:0,\39\.{\':\ \'},\39\#)$;\6
\&{end}\2\par
\P\D \37$\\{major}(\#)\S$\1\6
\&{if} $\\{out\_mode}>\\{errors\_only}$ \1\&{then}\5
$\\{show}(\#)$\2\2\par
\P\D \37$\\{minor}(\#)\S$\1\6
\&{if} $\\{out\_mode}=\\{verbose}$ \1\&{then}\6
\&{begin} \37$\\{showing}\K\\{true}$;\5
$\\{print}(\|a:0,\39\.{\':\ \'},\39\#)$;\6
\&{end}\2\2\par
\P\D \37$\\{error}(\#)\S$\1\6
\&{if} $\R\\{showing}$ \1\&{then}\5
$\\{show}(\#)$\6
\4\&{else} $\\{print}(\.{\'\ \'},\39\#)$\2\2\par
\Y\P$\4\X64:Translate the next command in the \.{DVI} file; \&{goto} 9999 with
$\\{do\_page}=\\{true}$ if it was \\{eop}; \&{goto} 9998 if premature
termination is needed\X\S$\6
\&{begin} \37$\|a\K\\{cur\_loc}$;\5
$\\{showing}\K\\{false}$;\5
$\|o\K\\{get\_byte}$;\5
$\|p\K\\{first\_par}(\|o)$;\5
\X65:Start translation of command \|o and \&{goto}  the appropriate label to
finish the job\X;\6
\4\\{fin\_set}: \37\X70:Finish a command that either sets or puts a character,
then \&{goto} \\{move\_right} or \\{done}\X;\6
\4\\{fin\_rule}: \37\X71:Finish a command that either sets or puts a rule, then
\&{goto} \\{move\_right} or \\{done}\X;\6
\4\\{move\_right}: \37\X72:Finish a command that sets $\|h\K\|h+\|q$, then \&
{goto} \\{done}\X;\6
\4\\{move\_down}: \37\X73:Finish a command that sets $\|v\K\|v+\|p$, then \&
{goto} \\{done}\X;\6
\4\\{show\_state}: \37\X74:Show the values of \\{ss}, \|h, \|v, \|w, \|x, \|y,
\|z, \\{hh}, and \\{vv}; then \&{goto} \\{done}\X;\6
\4\\{change\_font}: \37\X75:Finish a command that changes the current font\X;\6
\4\\{done}: \37\&{if} $\\{showing}$ \1\&{then}\5
$\\{print\_ln}(\.{\'\ \'})$;\2\6
\&{end}\par
\U section 63.

\M65. The multiway switch in \\{first\_par}, above, was organized by the length
of each command; the one in \\{do\_page} is organized by the semantics.

\Y\P$\4\X65:Start translation of command \|o and \&{goto}  the appropriate
label to finish the job\X\S$\6
\&{if} $\|o<\\{set\_char\_0}+128$ \1\&{then}\5
\X69:Translate a \\{set\_char} command\X\6
\4\&{else} \&{case} $\|o$ \1\&{of}\6
\4$\\{four\_cases}(\\{set1})$: \37\&{begin} \37$\\{major}(\.{\'set\'},\39\|o-\\
{set1}+1:0,\39\.{\'\ \'},\39\|p:0)$;\5
\&{goto} \37\\{fin\_set};\6
\&{end};\6
\4\\{set\_rule}: \37\&{begin} \37$\\{major}(\.{\'setrule\'})$;\5
\&{goto} \37\\{fin\_rule};\6
\&{end};\6
\4$\\{four\_cases}(\\{put1})$: \37\&{begin} \37$\\{major}(\.{\'put\'},\39\|o-\\
{put1}+1:0,\39\.{\'\ \'},\39\|p:0)$;\5
\&{goto} \37\\{done};\6
\&{end};\6
\4\\{put\_rule}: \37\&{begin} \37$\\{major}(\.{\'putrule\'})$;\5
\&{goto} \37\\{fin\_rule};\6
\&{end};\6
\hbox{\4}\X66:Cases for \\{nop}, \\{bop}, $\ldotss$, \\{pop}\X\6
\hbox{\4}\X67:Cases for horizontal and vertical motion\X\6
\4$\\{sixty\_four\_cases}(\\{fnt\_num\_0})$: \37\&{begin} \37$\\{major}(\.{\'
fntnum\'},\39\|p:0)$;\5
\&{goto} \37\\{change\_font};\6
\&{end};\6
\4$\\{four\_cases}(\\{fnt1})$: \37\&{begin} \37$\\{major}(\.{\'fnt\'},\39\|o-\\
{fnt1}+1:0,\39\.{\'\ \'},\39\|p:0)$;\5
\&{goto} \37\\{change\_font};\6
\&{end};\6
\4\\{xxx}: \37\X68:Translate an \\{xxx} command and \&{goto} \\{done}\X;\6
\4\\{pst}: \37\&{begin} \37$\\{error}(\.{\'pst\ occurred\ before\ eop\'})$;\5
\&{goto} \379998;\6
\&{end};\6
\4\\{undefined\_commands}: \37\&{begin} \37$\\{error}(\.{\'undefined\ command\
\'},\39\|o:0,\39\.{\'!\'})$;\5
\&{goto} \37\\{done};\6
\&{end};\2\6
\&{end}\2\par
\U section 64.

\M66. \P$\X66:Cases for \\{nop}, \\{bop}, $\ldotss$, \\{pop}\X\S$\6
\4\\{nop}: \37\&{begin} \37$\\{minor}(\.{\'nop\'})$;\5
\&{goto} \37\\{done};\6
\&{end};\6
\4\\{bop}: \37\&{begin} \37$\\{error}(\.{\'bop\ occurred\ before\ eop\'})$;\5
\&{goto} \379998;\6
\&{end};\6
\4\\{eop}: \37\&{begin} \37$\\{major}(\.{\'eop\'})$;\6
\&{if} $\|s\I0$ \1\&{then}\5
$\\{error}(\.{\'stack\ not\ empty\ at\ end\ of\ page\ (level\ \'},\39\|s:0,\39\.
{\')!\'})$;\2\6
$\\{do\_page}\K\\{true}$;\5
\&{goto} \379999;\6
\&{end};\6
\4\\{push}: \37\&{begin} \37$\\{major}(\.{\'push\'})$;\6
\&{if} $\|s=\\{max\_stack\_depth}$ \1\&{then}\5
$\\{error}(\.{\'deeper\ than\ claimed\ in\ postamble!\'})$;\2\6
\&{if} $\|s=\\{stack\_size}$ \1\&{then}\6
\&{begin} \37$\\{error}(\.{\'DVItype\ capacity\ exceeded\ (stack\ size=\'},\39\\
{stack\_size}:0,\39\.{\')\'})$;\5
\&{goto} \379998;\6
\&{end};\2\6
$\\{hstack}[\|s]\K\|h$;\5
$\\{vstack}[\|s]\K\|v$;\5
$\\{wstack}[\|s]\K\|w$;\5
$\\{xstack}[\|s]\K\|x$;\5
$\\{ystack}[\|s]\K\|y$;\5
$\\{zstack}[\|s]\K\|z$;\5
$\\{hhstack}[\|s]\K\\{hh}$;\5
$\\{vvstack}[\|s]\K\\{vv}$;\5
$\\{incr}(\|s)$;\5
$\\{ss}\K\|s-1$;\5
\&{goto} \37\\{show\_state};\6
\&{end};\6
\4\\{pop}: \37\&{begin} \37$\\{major}(\.{\'pop\'})$;\6
\&{if} $\|s=0$ \1\&{then}\5
$\\{error}(\.{\'(illegal\ at\ level\ zero)!\'})$\6
\4\&{else} \&{begin} \37$\\{decr}(\|s)$;\5
$\\{hh}\K\\{hhstack}[\|s]$;\5
$\\{vv}\K\\{vvstack}[\|s]$;\5
$\|h\K\\{hstack}[\|s]$;\5
$\|v\K\\{vstack}[\|s]$;\5
$\|w\K\\{wstack}[\|s]$;\5
$\|x\K\\{xstack}[\|s]$;\5
$\|y\K\\{ystack}[\|s]$;\5
$\|z\K\\{zstack}[\|s]$;\6
\&{end};\2\6
$\\{ss}\K\|s$;\5
\&{goto} \37\\{show\_state};\6
\&{end};\par
\U section 65.

\M67. Rounding to the nearest pixel is best done in the manner shown here, so
as
to be inoffensive to the eye: When the horizontal motion is small, like a
kern, \\{hh} changes by rounding the kern; but when the motion is large, \\{hh}
changes by rounding the true position \|h so that accumulated rounding errors
disappear.

Vertical motion is done similarly, but with the threshold between
``small'' and ``large'' increased by a factor of five. The idea is to make
fractions like ``$1\over2$'' round consistently, but to absorb accumulated
rounding errors in the baseline-skip moves.

\Y\P\D \37$\\{out\_space}(\#)\S$\1\6
\&{if} $\\{abs}(\|p)\G\\{font\_space}[\\{cur\_font}]$ \1\&{then}\6
\&{begin} \37$\\{out\_text}(\.{"\ "})$;\5
$\\{hh}\K\\{pixel\_round}(\|h+\|p)$;\6
\&{end}\6
\4\&{else} $\\{hh}\K\\{hh}+\\{pixel\_round}(\|p)$;\2\2\6
$\\{minor}(\#,\39\.{\'\ \'},\39\|p:0)$;\5
$\|q\K\|p$;\5
\&{goto} \37\\{move\_right}\par
\P\D \37$\\{out\_vmove}(\#)\S$\1\6
\&{if} $\\{abs}(\|p)\G5\ast\\{font\_space}[\\{cur\_font}]$ \1\&{then}\5
$\\{vv}\K\\{pixel\_round}(\|v+\|p)$\6
\4\&{else} $\\{vv}\K\\{vv}+\\{pixel\_round}(\|p)$;\2\2\6
$\\{major}(\#,\39\.{\'\ \'},\39\|p:0)$;\5
\&{goto} \37\\{move\_down}\par
\Y\P$\4\X67:Cases for horizontal and vertical motion\X\S$\6
\4$\\{four\_cases}(\\{right1})$: \37\&{begin} \37$\\{out\_space}(\.{\'right\'
},\39\|o-\\{right1}+1:0)$;\6
\&{end};\6
\4$\\{w0},\39\\{four\_cases}(\\{w1})$: \37\&{begin} \37$\|w\K\|p$;\5
$\\{out\_space}(\.{\'w\'},\39\|o-\\{w0}:0)$;\6
\&{end};\6
\4$\\{x0},\39\\{four\_cases}(\\{x1})$: \37\&{begin} \37$\|x\K\|p$;\5
$\\{out\_space}(\.{\'x\'},\39\|o-\\{x0}:0)$;\6
\&{end};\6
\4$\\{four\_cases}(\\{down1})$: \37\&{begin} \37$\\{out\_vmove}(\.{\'down\'},\3
9\|o-\\{down1}+1:0)$;\6
\&{end};\6
\4$\\{y0},\39\\{four\_cases}(\\{y1})$: \37\&{begin} \37$\|y\K\|p$;\5
$\\{out\_vmove}(\.{\'y\'},\39\|o-\\{y0}:0)$;\6
\&{end};\6
\4$\\{z0},\39\\{four\_cases}(\\{z1})$: \37\&{begin} \37$\|z\K\|p$;\5
$\\{out\_vmove}(\.{\'z\'},\39\|o-\\{z0}:0)$;\6
\&{end};\par
\U section 65.

\M68. \P$\X68:Translate an \\{xxx} command and \&{goto} \\{done}\X\S$\6
\&{begin} \37$\\{major}(\.{\'xxx\'}\.{\'\'})$;\5
$\\{bad\_char}\K\\{false}$;\6
\&{for} $\|k\K1\mathrel{\&{to}}\|p$ \1\&{do}\6
\&{begin} \37$\|q\K\\{get\_byte}$;\6
\&{if} $(\|q\G\.{"!"})\W(\|q\L\.{"\~"})$ \1\&{then}\6
\&{begin} \37\&{if} $\\{showing}$ \1\&{then}\5
$\\{print}(\\{xchr}[\|q])$;\2\6
\&{end}\6
\4\&{else} $\\{bad\_char}\K\\{true}$;\2\6
\&{end};\2\6
\&{if} $\\{showing}$ \1\&{then}\5
$\\{print}(\.{\'\'}\.{\'\'})$;\2\6
\&{if} $\\{bad\_char}$ \1\&{then}\5
$\\{error}(\.{\'non-ascii\ character\ in\ xxx\ command!\'})$;\2\6
\&{end}\par
\U section 65.

\M69. \P$\X69:Translate a \\{set\_char} command\X\S$\6
\&{begin} \37\&{if} $(\|o>\.{"\ "})\W(\|o\L\.{"\~"})$ \1\&{then}\6
\&{begin} \37$\\{out\_text}(\|p)$;\5
$\\{minor}(\.{\'setchar\'},\39\|p:0)$;\6
\&{end}\6
\4\&{else} $\\{major}(\.{\'setchar\'},\39\|p:0)$;\2\6
\&{goto} \37\\{fin\_set};\6
\&{end}\par
\U section 65.

\M70. \P$\X70:Finish a command that either sets or puts a character, then \&
{goto} \\{move\_right} or \\{done}\X\S$\6
\&{if} $\\{font\_ec}[\\{cur\_font}]=256$ \1\&{then}\5
$\|p\K256$;\C{width computation for oriental fonts}\2\6
\&{if} $(\|p<\\{font\_bc}[\\{cur\_font}])\V(\|p>\\{font\_ec}[\\{cur\_font}])$ \1
\&{then}\5
$\|q\K\\{invalid\_width}$\6
\4\&{else} $\|q\K\\{char\_width}(\\{cur\_font})(\|p)$;\2\6
\&{if} $\|q=\\{invalid\_width}$ \1\&{then}\6
\&{begin} \37$\\{error}(\.{\'character\ \'},\39\|p:0,\39\.{\'\ invalid\ in\
font\ \'})$;\5
$\\{print\_font}(\\{cur\_font})$;\6
\&{if} $\\{cur\_font}\I\\{nf}$ \1\&{then}\5
$\\{print}(\.{\'!\'})$;\2\6
\&{end};\2\6
\&{if} $\|o\G\\{put1}$ \1\&{then}\5
\&{goto} \37\\{done};\2\6
\&{if} $\|q=\\{invalid\_width}$ \1\&{then}\5
$\|q\K0$\6
\4\&{else} $\\{hh}\K\\{hh}+\\{char\_pixel\_width}(\\{cur\_font})(\|p)$;\2\6
\&{goto} \37\\{move\_right}\par
\U section 64.

\M71. \P$\X71:Finish a command that either sets or puts a rule, then \&{goto} \\
{move\_right} or \\{done}\X\S$\6
$\|q\K\\{signed\_quad}$;\6
\&{if} $\\{showing}$ \1\&{then}\6
\&{begin} \37$\\{print}(\.{\'\ height\ \'},\39\|p:0,\39\.{\',\ width\ \'},\39\|
q:0)$;\6
\&{if} $(\|p\L0)\V(\|q\L0)$ \1\&{then}\5
$\\{print}(\.{\'\ (invisible)\'})$\6
\4\&{else} $\\{print}(\.{\'\ (\'},\39\\{rule\_pixels}(\|p):0,\39\.{\'x\'},\39\\
{rule\_pixels}(\|q):0,\39\.{\'\ pixels)\'})$;\2\6
\&{end};\2\6
\&{if} $\|o=\\{put\_rule}$ \1\&{then}\5
\&{goto} \37\\{done};\2\6
$\\{hh}\K\\{hh}+\\{rule\_pixels}(\|q)$;\5
\&{goto} \37\\{move\_right}\par
\U section 64.

\M72. Since \.{DVItype} is intended to diagnose strange errors, it checks
carefully to make sure that \|h and \|v do not get out of range.
Normal \.{DVI}-reading programs need not do this.

\Y\P\D \37$\\{infinity}\S\O17777777777$\C{$\infty$ (approximately)}\par
\Y\P$\4\X72:Finish a command that sets $\|h\K\|h+\|q$, then \&{goto} \\
{done}\X\S$\6
\&{if} $(\|h>0)\W(\|q>0)$ \1\&{then}\6
\&{if} $\|h>\\{infinity}-\|q$ \1\&{then}\6
\&{begin} \37$\\{error}(\.{\'arithmetic\ overflow!\ parameter\ changed\ from\ \'
},\39\|q:0,\39\.{\'\ to\ \'},\39\\{infinity}-\|h:0)$;\5
$\|q\K\\{infinity}-\|h$;\6
\&{end};\2\2\6
\&{if} $(\|h<0)\W(\|q<0)$ \1\&{then}\6
\&{if} $-\|h>\|q+\\{infinity}$ \1\&{then}\6
\&{begin} \37$\\{error}(\.{\'arithmetic\ overflow!\ parameter\ changed\ from\ \'
},\39\|q:0,\39\.{\'\ to\ \'},\39(-\|h)-\\{infinity}:0)$;\5
$\|q\K(-\|h)-\\{infinity}$;\6
\&{end};\2\2\6
\&{if} $\\{showing}$ \1\&{then}\6
\&{begin} \37$\\{print}(\.{\'\ h:=\'},\39\|h:0)$;\6
\&{if} $\|q\G0$ \1\&{then}\5
$\\{print}(\.{\'+\'})$;\2\6
$\\{print}(\|q:0,\39\.{\'=\'},\39\|h+\|q:0,\39\.{\',\ hh:=\'},\39\\{hh}:0)$;\6
\&{end};\2\6
$\|h\K\|h+\|q$;\6
\&{if} $\\{abs}(\|h)>\\{max\_h}$ \1\&{then}\6
\&{begin} \37$\\{error}(\.{\'warning:\ |h|>\'},\39\\{max\_h}:0,\39\.{\'!\'})$;\5
$\\{max\_h}\K\\{abs}(\|h)$;\6
\&{end};\2\6
\&{goto} \37\\{done}\par
\U section 64.

\M73. \P$\X73:Finish a command that sets $\|v\K\|v+\|p$, then \&{goto} \\
{done}\X\S$\6
\&{if} $(\|v>0)\W(\|p>0)$ \1\&{then}\6
\&{if} $\|v>\\{infinity}-\|p$ \1\&{then}\6
\&{begin} \37$\\{error}(\.{\'arithmetic\ overflow!\ parameter\ changed\ from\ \'
},\39\|p:0,\39\.{\'\ to\ \'},\39\\{infinity}-\|v:0)$;\5
$\|p\K\\{infinity}-\|v$;\6
\&{end};\2\2\6
\&{if} $(\|v<0)\W(\|p<0)$ \1\&{then}\6
\&{if} $-\|v>\|p+\\{infinity}$ \1\&{then}\6
\&{begin} \37$\\{error}(\.{\'arithmetic\ overflow!\ parameter\ changed\ from\ \'
},\39\|p:0,\39\.{\'\ to\ \'},\39(-\|v)-\\{infinity}:0)$;\5
$\|p\K(-\|v)-\\{infinity}$;\6
\&{end};\2\2\6
\&{if} $\\{showing}$ \1\&{then}\6
\&{begin} \37$\\{print}(\.{\'\ v:=\'},\39\|v:0)$;\6
\&{if} $\|p\G0$ \1\&{then}\5
$\\{print}(\.{\'+\'})$;\2\6
$\\{print}(\|p:0,\39\.{\'=\'},\39\|v+\|p:0,\39\.{\',\ vv:=\'},\39\\{vv}:0)$;\6
\&{end};\2\6
$\|v\K\|v+\|p$;\6
\&{if} $\\{abs}(\|v)>\\{max\_v}$ \1\&{then}\6
\&{begin} \37$\\{error}(\.{\'warning:\ |v|>\'},\39\\{max\_v}:0,\39\.{\'!\'})$;\5
$\\{max\_v}\K\\{abs}(\|v)$;\6
\&{end};\2\6
\&{goto} \37\\{done}\par
\U section 64.

\M74. \P$\X74:Show the values of \\{ss}, \|h, \|v, \|w, \|x, \|y, \|z, \\{hh},
and \\{vv}; then \&{goto} \\{done}\X\S$\6
\&{if} $\\{showing}$ \1\&{then}\6
\&{begin} \37$\\{print\_ln}(\.{\'\ \'})$;\5
$\\{print}(\.{\'level\ \'},\39\\{ss}:0,\39\.{\':(h=\'},\39\|h:0,\39\.{\',v=\'
},\39\|v:0,\39\.{\',w=\'},\39\|w:0,\39\.{\',x=\'},\39\|x:0,\39\.{\',y=\'},\39\|
y:0,\39\.{\',z=\'},\39\|z:0,\39\.{\',hh=\'},\39\\{hh}:0,\39\.{\',vv=\'},\39\\
{vv}:0,\39\.{\')\'})$;\6
\&{end};\2\6
\&{goto} \37\\{done}\par
\U section 64.

\M75. \P$\X75:Finish a command that changes the current font\X\S$\6
$\\{font\_num}[\\{nf}]\K\|p$;\5
$\\{cur\_font}\K0$;\6
\&{while} $\\{font\_num}[\\{cur\_font}]\I\|p$ \1\&{do}\5
$\\{incr}(\\{cur\_font})$;\2\6
\&{if} $\\{showing}$ \1\&{then}\6
\&{begin} \37$\\{print}(\.{\'\ current\ font\ is\ \'})$;\5
$\\{print\_font}(\\{cur\_font})$;\6
\&{end}\2\par
\U section 64.

\N76.  Finding the postamble and the starting page.
\.{DVItype} makes a first pass over the given \.{DVI} file in order to
(a)@count the total number of pages (\\{page\_count}); (b)@find the
starting byte of the postamble (\\{pst\_loc}) and prepare to read it;
(c)@find the starting byte of the \\{bop} command that corresponds to the
specified starting page (\\{start\_loc}).

If \\{random\_reading} is true, this can be done by looking at only a
small percentage of the total number of bytes in a typical \.{DVI}
file. Otherwise the program marches through the whole file.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{page\_count}: \37\\{integer};\C{the number of \\{bop} commands in the \.
{DVI} file}\6
\4\\{pst\_loc}: \37\\{integer};\C{address of the \\{pst} byte}\6
\4\\{start\_loc}: \37\\{integer};\C{address of the \\{bop} byte where
translation should start}\par

\M77. If the \.{DVI} file is badly malformed, the whole process is aborted and
\.{DVItype} gives up, after issuing an error message about the symptoms
that were noticed.

\Y\P\D \37$\\{abort}(\#)\S$\1\6
\&{begin} \37$\\{print}(\.{\'\ \'},\39\#)$;\5
\&{goto} \37\\{final\_end};\6
\&{end}\2\par
\P\D \37$\\{bad\_dvi}(\#)\S\\{abort}(\.{\'Bad\ DVI\ file:\ \'},\39\#,\39\.{\'!\'
})$\par

\M78. Some integer variables are used to control this initialization.

\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4$\|k,\39\|m,\39\|n,\39\|p,\39\|q,\39\|r$: \37\\{integer};\C{pointers into the
\.{DVI} file, 	or miscellaneous registers for temporary use}\par

\M79. Here is the program that takes care of the first ``pass'':

\Y\P$\4\X79:Do a quick look at the file, moving to byte number $\\{pst\_
loc}+5$\X\S$\6
\\{open\_dvi\_file};\5
$\\{pst\_loc}\K-1$;\5
$\\{start\_loc}\K-1$;\5
$\\{page\_count}\K0$;\6
\&{if} $\\{random\_reading}$ \1\&{then}\6
\&{begin} \37\X80:Find \\{pst\_loc}, working backwards from the end of the
file\X;\6
\X81:Count the pages and find \\{start\_loc}\X;\6
$\\{move\_to\_byte}(\\{pst\_loc}+5)$;\6
\&{end}\6
\4\&{else} \&{begin} \37\X82:Go through the file, counting pages and finding \\
{start\_loc} and \\{pst\_loc}\X;\6
\X84:Check the pointer to the previous \\{bop}\X;\6
\&{end}\2\par
\U section 95.

\M80. \P$\X80:Find \\{pst\_loc}, working backwards from the end of the
file\X\S$\6
$\|n\K\\{dvi\_length}$;\6
\&{if} $\|n<42$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'only\ \'},\39\|n:0,\39\.{\'\ bytes\ long\'})$;\2\6
$\|m\K\|n-4$;\6
\1\&{repeat} \37\&{if} $\|m=0$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'all\ 223s\'})$;\2\6
$\\{move\_to\_byte}(\|m)$;\5
$\|k\K\\{get\_byte}$;\5
$\\{decr}(\|m)$;\6
\4\&{until}\5
$\|k\I223$;\2\6
\&{if} $\|k\I\\{id\_byte}$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'ID\ byte\ is\ \'},\39\|k:0)$;\2\6
$\\{move\_to\_byte}(\|m-3)$;\5
$\|q\K\\{signed\_quad}$;\6
\&{if} $(\|q<0)\V(\|q>\|m-36)$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'pst\ pointer\ \'},\39\|q:0,\39\.{\'\ at\ byte\ \'},\39\|
m-3:0)$;\2\6
$\\{move\_to\_byte}(\|q)$;\5
$\|k\K\\{get\_byte}$;\6
\&{if} $\|k\I\\{pst}$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'byte\ \'},\39\|q:0,\39\.{\'\ is\ not\ pst\'})$;\2\6
$\\{pst\_loc}\K\|q$\par
\U section 79.

\M81. At this point in the program, the \.{DVI} file is positioned ready to
read byte $\\{pst\_loc}+1$. Normal \.{DVI}-reading programs would now proceed
immediately to look at the postamble; and there would be no need to
count the pages or to search for \\{start\_loc} if all pages of the file are
to be processed. Thus, the loop shown here is not typical; it is
rather specific to \.{DVItype}'s mission of checking the file carefully.

\Y\P$\4\X81:Count the pages and find \\{start\_loc}\X\S$\6
\1\&{repeat} \37$\|p\K\\{signed\_quad}$;\C{now \|q points to a \\{pst} or \\
{bop} command; \|p is prev pointer}\6
\&{if} $(\|p>\|q-46)\W(\|p\G0)$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'page\ link\ \'},\39\|p:0,\39\.{\'\ after\ byte\ \'},\39\|
q:0)$;\2\6
\&{if} $\|p\G0$ \1\&{then}\6
\&{begin} \37$\|q\K\|p$;\5
$\\{move\_to\_byte}(\|q)$;\5
$\|k\K\\{get\_byte}$;\6
\&{if} $\|k=\\{bop}$ \1\&{then}\5
$\\{incr}(\\{page\_count})$\6
\4\&{else} $\\{bad\_dvi}(\.{\'byte\ \'},\39\|q:0,\39\.{\'\ is\ not\ bop\'})$;\2
\6
\&{for} $\|k\K0\mathrel{\&{to}}9$ \1\&{do}\5
$\\{count}[\|k]\K\\{signed\_quad}$;\2\6
\&{if} $\\{start\_match}$ \1\&{then}\5
$\\{start\_loc}\K\|q$;\2\6
\&{end}\6
\4\&{else} \&{if} $\|q>0$ \1\&{then}\6
\&{begin} \37$\\{move\_to\_byte}(0)$;\6
\&{while} $\\{cur\_loc}<\|q$ \1\&{do}\6
\&{begin} \37$\|k\K\\{get\_byte}$;\6
\&{if} $\|k\I\\{nop}$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'byte\ \'},\39\\{cur\_loc}-1:0,\39\.{\'\ is\ not\ nop\'})$;\2
\6
\&{end};\2\6
\&{end};\2\2\6
\4\&{until}\5
$\|p<0$\2\par
\U section 79.

\M82. If the first pass is really a pass ($\\{random\_reading}=\\{false}$), we
do it
in slower motion:

\Y\P$\4\X82:Go through the file, counting pages and finding \\{start\_loc} and
\\{pst\_loc}\X\S$\6
\1\&{repeat} \37\&{if} $\\{eof}(\\{dvi\_file})$ \1\&{then}\5
$\|k\K0$\ \&{else} $\|k\K\\{get\_byte}$;\2\6
\4\&{until}\5
$\|k\I\\{nop}$;\2\6
\&{if} $(\|k\I\\{bop})\W(\|k\I\\{pst})$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'first\ non-nop\ byte\ is\ \'},\39\|k:0)$;\2\6
$\|p\K-1$;\6
\&{while} $\\{pst\_loc}<0$ \1\&{do}\6
\&{begin} \37$\|m\K\\{first\_par}(\|k)$;\6
\&{if} $\|k=\\{bop}$ \1\&{then}\5
\X83:Pass \\{bop} command\X\6
\4\&{else} \&{if} $(\|k=\\{set\_rule})\V(\|k=\\{put\_rule})$ \1\&{then}\5
$\|m\K\\{signed\_quad}$\6
\4\&{else} \&{if} $\|k=\\{pst}$ \1\&{then}\5
$\\{pst\_loc}\K\\{cur\_loc}-1$\6
\4\&{else} \&{if} $\|k=\\{xxx}$ \1\&{then}\6
\&{for} $\|k\K1\mathrel{\&{to}}\|m$ \1\&{do}\5
$\|n\K\\{get\_byte}$;\2\2\2\2\2\6
\&{if} $\\{eof}(\\{dvi\_file})$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'postamble\ unfindable\'})$;\2\6
\&{if} $\\{pst\_loc}<0$ \1\&{then}\5
$\|k\K\\{get\_byte}$;\2\6
\&{end}\2\par
\U section 79.

\M83. \P$\X83:Pass \\{bop} command\X\S$\6
\&{begin} \37$\\{incr}(\\{page\_count})$;\6
\&{for} $\|k\K0\mathrel{\&{to}}9$ \1\&{do}\5
$\\{count}[\|k]\K\\{signed\_quad}$;\2\6
\&{if} $(\\{start\_loc}<0)\W\\{start\_match}$ \1\&{then}\5
$\\{start\_loc}\K\\{cur\_loc}-41$;\2\6
\X84:Check the pointer to the previous \\{bop}\X;\6
$\|p\K\\{cur\_loc}-45$;\6
\&{end}\par
\U section 82.

\M84. During this pass, \|p points to the previous \\{bop} command in the file.

\Y\P$\4\X84:Check the pointer to the previous \\{bop}\X\S$\6
\&{begin} \37$\|k\K\\{signed\_quad}$;\6
\&{if} $\|k\I\|p$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'backpointer\ in\ byte\ \'},\39\\{cur\_loc}-4:0,\39\.{\'\
should\ be\ \'},\39\|p:0)$;\2\6
\&{end}\par
\U sections 79 and 83.

\N85.  Reading the postamble.
Now imagine that we are reading the \.{DVI} file and positioned just
four bytes after the \\{pst} command. That, in fact, is the situation,
when the following part of \.{DVItype} is called upon to read, translate,
and check the rest of the postamble.

\Y\P$\4\X85:Read, translate, and check the postamble\X\S$\6
\&{begin} \37$\\{print\_ln}(\.{\'Postamble\ starts\ at\ byte\ \'},\39\\{pst\_
loc}:0,\39\.{\'.\'})$;\5
\X86:Compute the conversion factor\X;\6
$\\{max\_v}\K\\{signed\_quad}$;\5
$\\{max\_h}\K\\{signed\_quad}$;\5
$\\{max\_stack\_depth}\K\\{get\_two\_bytes}$;\5
$\\{print}(\.{\'maxv=\'},\39\\{max\_v}:0,\39\.{\',\ maxh=\'},\39\\{max\_h}:0,\3
9\.{\',\ maxstackdepth=\'},\39\\{max\_stack\_depth}:0)$;\5
$\|m\K\\{get\_two\_bytes}$;\5
$\\{print}(\.{\',\ totalpages=\'},\39\|m:0)$;\6
\&{if} $\|m=\\{page\_count}$ \1\&{then}\5
$\\{print\_ln}(\.{\'\ \'})$\6
\4\&{else} $\\{print\_ln}(\.{\'\ (should\ be\'},\39\\{page\_count}:0,\39\.{\'
!)\'})$;\2\6
\X88:Process the font definitions\X;\6
\X87:Make sure that the end of the file is well-formed\X;\6
\&{end}\par
\U section 95.

\M86. The conversion factor \\{conv} is figured as follows: There are exactly
$\|n/\|d$ \.{DVI} units per decimicron, and 254000 decimicrons per inch,
and \\{resolution} pixels per inch. Then we have to adjust this
by the stated amount of magnification.

\Y\P$\4\X86:Compute the conversion factor\X\S$\6
$\|n\K\\{signed\_quad}$;\5
$\|m\K\\{signed\_quad}$;\6
\&{if} $\|n\L0$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'numerator\ is\ \'},\39\|n:0)$;\2\6
\&{if} $\|m\L0$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'denominator\ is\ \'},\39\|m:0)$;\2\6
$\\{print\_ln}(\.{\'numerator/denominator=\'},\39\|n:0,\39\.{\'/\'},\39\|
m:0)$;\5
$\\{conv}\K(\|n/254000.0)\ast(\\{resolution}/\|m)$;\5
$\|n\K\\{signed\_quad}$;\6
\&{if} $\\{new\_mag}>0$ \1\&{then}\5
$\|n\K\\{new\_mag}$\6
\4\&{else} \&{if} $\|n\L0$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'magnification\ is\ \'},\39\|n:0)$;\2\2\6
$\\{conv}\K\\{conv}\ast(\|n/1000.0)$;\5
$\\{print\_ln}(\.{\'magnification=\'},\39\|n:0,\39\.{\';\ \'},\39\\
{conv}:16:8,\39\.{\'\ pixels\ per\ DVI\ unit\'})$\par
\U section 85.

\M87. When we get to the present code, the phony `$-1$' font number has
just been read.

\Y\P$\4\X87:Make sure that the end of the file is well-formed\X\S$\6
$\|q\K\\{signed\_quad}$;\6
\&{if} $\|q\I\\{pst\_loc}$ \1\&{then}\5
$\\{print\_ln}(\.{\'pst\ pointer\ in\ byte\ \'},\39\\{cur\_loc}-4:0,\39\.{\'\
should\ be\ \'},\39\\{pst\_loc}:0,\39\.{\'!\'})$;\2\6
$\|m\K\\{get\_byte}$;\6
\&{if} $\|m\I\\{id\_byte}$ \1\&{then}\5
$\\{print\_ln}(\.{\'identification\ in\ byte\ \'},\39\\{cur\_loc}-1:0,\39\.{\'\
should\ be\ \'},\39\\{id\_byte}:0,\39\.{\'!\'})$;\2\6
$\|k\K\\{cur\_loc}$;\5
$\|m\K223$;\6
\&{while} $(\|m=223)\W\R\\{eof}(\\{dvi\_file})$ \1\&{do}\5
$\|m\K\\{get\_byte}$;\2\6
\&{if} $\R\\{eof}(\\{dvi\_file})$ \1\&{then}\5
$\\{print\_ln}(\.{\'signature\ in\ byte\ \'},\39\\{cur\_loc}-1:0,\39\.{\'\
should\ be\ 223!\'})$\6
\4\&{else} \&{if} $\\{cur\_loc}<\|k+4$ \1\&{then}\5
$\\{print\_ln}(\.{\'not\ enough\ signature\ bytes\ at\ end\ of\ file\ (\'},\39\\
{cur\_loc}-\|k:0,\39\.{\')\'})$;\2\2\par
\U section 85.

\M88. The most complicated part of the postamble processing is the part
that we still have to tackle, namely the definitions of the fonts.

\Y\P$\4\X88:Process the font definitions\X\S$\6
$\\{font\_num}[\\{nf}]\K\\{signed\_quad}$;\6
\&{while} $\\{font\_num}[\\{nf}]\I-1$ \1\&{do}\6
\&{begin} \37\&{if} $\\{eof}(\\{dvi\_file})$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'endless\ font\ definitions\'})$;\2\6
\&{if} $\\{nf}=\\{max\_fonts}$ \1\&{then}\5
$\\{abort}(\.{\'DVItype\ capacity\ exceeded\ (max\ fonts=\'},\39\\{max\_
fonts}:0,\39\.{\')!\'})$;\2\6
$\\{print}(\.{\'Font\ \'},\39\\{font\_num}[\\{nf}]:0,\39\.{\':\ \'})$;\5
$\|m\K\\{signed\_quad}$;\C{the check sum}\6
$\|q\K\\{signed\_quad}$;\C{the scaled size}\6
\X89:Move font name into storage and into \\{cur\_name}\X;\6
\X93:Load the font unless there are problems\X;\6
$\\{font\_num}[\\{nf}]\K\\{signed\_quad}$;\6
\&{end}\2\par
\U section 85.

\M89. We substitute question marks for non-ascii characters in the font name.

\Y\P$\4\X89:Move font name into storage and into \\{cur\_name}\X\S$\6
$\|p\K\\{get\_byte}$;\C{length of the area/directory spec}\6
$\|n\K\\{get\_byte}$;\C{length of the font name proper}\6
\&{if} $\\{font\_name}[\\{nf}]+\|n+\|p>\\{name\_size}$ \1\&{then}\5
$\\{abort}(\.{\'DVItype\ capacity\ exceeded\ (name\ size=\'},\39\\{name\_
size}:0,\39\.{\')!\'})$;\2\6
\&{if} $\|n+\|p=0$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'null\ font\ name\'})$;\2\6
$\\{font\_name}[\\{nf}+1]\K\\{font\_name}[\\{nf}]+\|n+\|p$;\6
\&{for} $\|k\K\\{font\_name}[\\{nf}]\mathrel{\&{to}}\\{font\_name}[\\{nf}+1]-1$
\1\&{do}\6
\&{begin} \37$\|r\K\\{get\_byte}$;\6
\&{if} $(\|r<\.{"\ "})\V(\|r>\.{"\~"})$ \1\&{then}\5
$\\{names}[\|k]\K\.{"?"}$\ \&{else} $\\{names}[\|k]\K\|r$;\2\6
\&{end};\2\6
$\\{incr}(\\{nf})$;\5
$\\{print\_font}(\\{nf}-1)$;\5
$\\{decr}(\\{nf})$;\5
\X92:Move font name into the \\{cur\_name} string\X\par
\U section 88.

\M90. If $\|p=0$, i.e., if no font directory has been specified, \.{DVItype}
is supposed to use the default font directory, which is a
system-dependent place where the standard fonts are kept.
The string variable \\{default\_directory} contains the name of this area.

\Y\P\D \37$\\{default\_directory\_name}\S\.{\'<SYS.FONTS>\'}$\C{change this to
the correct name}\par
\P\D \37$\\{default\_directory\_name\_length}=11$\C{change this to the correct
length}\par
\Y\P$\4\X9:Globals in the outer block\X\mathrel{+}\S$\6
\4\\{default\_directory}: \37\&{packed} \37\&{array} $[1:\\{default\_directory\_
name\_length}]$ \1\&{of}\5
\\{char};\2\par

\M91. \P$\X10:Set initial values\X\mathrel{+}\S$\6
$\\{default\_directory}\K\\{default\_directory\_name}$;\par

\M92. The string \\{cur\_name} is supposed to be set to the external name of
the
\.{TFM} file for the current font. This usually means that we need to
prepend the name of the default directory, and
to append the suffix `\.{.TFM}'. Furthermore, we change lower case letters
to upper case, since \\{cur\_name} is a \PASCAL\ string.

\Y\P$\4\X92:Move font name into the \\{cur\_name} string\X\S$\6
\&{for} $\|k\K1\mathrel{\&{to}}\\{name\_length}$ \1\&{do}\5
$\\{cur\_name}[\|k]\K\.{\'\ \'}$;\2\6
\&{if} $\|p=0$ \1\&{then}\6
\&{begin} \37\&{for} $\|k\K1\mathrel{\&{to}}\\{default\_directory\_name\_
length}$ \1\&{do}\5
$\\{cur\_name}[\|k]\K\\{default\_directory}[\|k]$;\2\6
$\|r\K\\{default\_directory\_name\_length}$;\6
\&{end}\6
\4\&{else} $\|r\K0$;\2\6
\&{for} $\|k\K\\{font\_name}[\\{nf}]\mathrel{\&{to}}\\{font\_name}[\\{nf}+1]-1$
\1\&{do}\6
\&{begin} \37$\\{incr}(\|r)$;\6
\&{if} $\|r+4>\\{name\_length}$ \1\&{then}\5
$\\{abort}(\.{\'Font\ name\ is\ too\ long!\'})$\1\6
\&{if} $(\\{names}[\|k]\G\.{"a"})\W(\\{names}[\|k]\L\.{"z"})$ \1\&{then}\5
$\\{cur\_name}[\|r]\K\\{xchr}[\\{names}[\|k]-\O40]$\6
\4\&{else} $\\{cur\_name}[\|r]\K\\{xchr}[\\{names}[\|k]]$;\2\2\2\6
\&{end};\2\6
$\\{cur\_name}[\|r+1]\K\.{\'.\'}$;\5
$\\{cur\_name}[\|r+2]\K\.{\'T\'}$;\5
$\\{cur\_name}[\|r+3]\K\.{\'F\'}$;\5
$\\{cur\_name}[\|r+4]\K\.{\'M\'}$\par
\U section 89.

\M93. \P$\X93:Load the font unless there are problems\X\S$\6
$\|k\K0$;\6
\&{while} $\\{font\_num}[\|k]\I\\{font\_num}[\\{nf}]$ \1\&{do}\5
$\\{incr}(\|k)$;\2\6
\&{if} $\|k<\\{nf}$ \1\&{then}\5
$\\{print\_ln}(\.{\'---not\ loaded,\ this\ number\ already\ used!\'})$\6
\4\&{else} \&{begin} \37\\{open\_tfm\_file};\6
\&{if} $\\{eof}(\\{tfm\_file})$ \1\&{then}\5
$\\{print\_ln}(\.{\'---not\ loaded,\ TFM\ file\ can\'}\.{\'t\ be\ opened!\'})$\6
\4\&{else} \&{begin} \37\&{if} $(\|q\L0)\V(\|q\G\O1000000000)$ \1\&{then}\5
$\\{print\_ln}(\.{\'---not\ loaded,\ bad\ scale\ (\'},\39\|q:0,\39\.{\')!\'})$\6
\4\&{else} \&{if} $\\{in\_TFM}(\|q)$ \1\&{then}\5
\X94:Finish loading the new font info\X;\2\2\6
\&{end};\2\6
\&{end}\2\par
\U section 88.

\M94. \P$\X94:Finish loading the new font info\X\S$\6
\&{begin} \37$\\{font\_space}[\\{nf}]\K\|q\mathbin{\&{div}}6$;\C{this is a
3-unit ``thin space''}\6
\&{if} $(\|m\I0)\W(\\{tfm\_check\_sum}\I0)\W(\|m\I\\{tfm\_check\_sum})$ \1\&
{then}\6
\&{begin} \37$\\{print\_ln}(\.{\'---loaded\ but\ beware:\ check\ sums\ do\ not\
agree!\'})$;\5
$\\{print\_ln}(\.{\'\ \ \ (\'},\39\|m:0,\39\.{\'\ vs.\ \'},\39\\{tfm\_check\_
sum}:0,\39\.{\')\'})$;\6
\&{end}\6
\4\&{else} $\\{print\_ln}(\.{\'---loaded\ at\ size\ \'},\39\|q:0,\39\.{\'\ DVI\
units\'})$;\2\6
$\\{incr}(\\{nf})$;\C{this makes the new font ``official''}\6
\&{end}\par
\U section 93.

\N95.  The main program.
Now we are ready to put it all together. This is where \.{DVItype} starts,
and where it ends.

\Y\P\&{begin} \37\\{initialize};\C{get all variables initialized}\6
\\{dialog};\C{set up all the options}\6
\X79:Do a quick look at the file, moving to byte number $\\{pst\_loc}+5$\X;\6
\X85:Read, translate, and check the postamble\X;\6
\&{if} $\\{start\_loc}<0$ \1\&{then}\5
$\\{print\_ln}(\.{\'The\ starting\ page\ could\ not\ be\ found!\'})$\6
\4\&{else} \&{begin} \37\&{if} $\\{random\_reading}$ \1\&{then}\5
$\\{move\_to\_byte}(\\{start\_loc})$\6
\4\&{else} \&{begin} \37\\{open\_dvi\_file};\C{prepare for second pass}\6
\&{while} $\\{cur\_loc}<\\{start\_loc}$ \1\&{do}\5
$\|n\K\\{get\_byte}$;\2\6
\&{end};\2\6
\X96:Translate up to \\{max\_pages} pages; \&{goto} \\{final\_end} if the
postamble is reached\X;\6
\&{end};\2\6
\4\\{final\_end}: \37\&{end}.\par

\M96. The code shown here uses a convention that has proved to be useful:
If the starting page was specified as, e.g., `\.{1.*.-5}', then
all page numbers in the file are displayed by showing the values of
counts 0, 1, and@2, separated by dots. Such numbers can, for example,
be displayed on the console of a printer when it is working on that
page.

\Y\P$\4\X96:Translate up to \\{max\_pages} pages; \&{goto} \\{final\_end} if
the postamble is reached\X\S$\6
\&{while} $\\{max\_pages}>0$ \1\&{do}\6
\&{begin} \37$\\{decr}(\\{max\_pages})$;\6
\1\&{repeat} \37$\|k\K\\{get\_byte}$;\6
\4\&{until}\5
$\|k\I\\{nop}$;\2\6
\&{if} $\|k=\\{pst}$ \1\&{then}\5
\&{goto} \37\\{final\_end};\2\6
\&{if} $\|k\I\\{bop}$ \1\&{then}\5
$\\{bad\_dvi}(\.{\'command\ at\ byte\ \'},\39\\{cur\_loc}-1:0,\39\.{\'\ is\
not\ bop\'})$;\2\6
$\\{print\_ln}(\.{\'\ \'})$;\5
$\\{print}(\\{cur\_loc}-1:0,\39\.{\':\ beginning\ of\ page\ \'})$;\6
\&{for} $\|k\K0\mathrel{\&{to}}\\{start\_vals}$ \1\&{do}\6
\&{begin} \37$\\{print}(\\{signed\_quad}:0)$;\6
\&{if} $\|k<\\{start\_vals}$ \1\&{then}\5
$\\{print}(\.{\'.\'})$\6
\4\&{else} $\\{print\_ln}(\.{\'\ \'})$;\2\6
\&{end};\2\6
\&{for} $\|k\K\\{start\_vals}+1\mathrel{\&{to}}10$ \1\&{do}\5
$\|n\K\\{signed\_quad}$;\C{ignore remaining counts and the back pointer}\2\6
\&{if} $\R\\{do\_page}$ \1\&{then}\5
$\\{abort}(\.{\'page\ ended\ unexpectedly!\'})$;\2\6
\&{end}\2\par
\U section 95.

\N97.  System-dependent changes.
This module should be replaced, if necessary, by changes to the program
that are necessary to make \.{DVItype} work at a particular installation.
It is usually best to design your change file so that all changes to
previous modules preserve the module numbering; then everybody's version
will be consistent with the printed program. More extensive changes,
which introduce new modules, can be inserted here; then only the index
itself will get a new module number.

\N98.  Index.
Pointers to error messages appear here together with the section numbers
where each ident\-i\-fier is used.

\inx
\+\|{a}, \[24], \[63].
\+\\{abort}, \[77], 88, 89, 92, 96.
\+\\{abs}, 60, 67, 72, 73.
\+\\{alpha}, \[31], 34, 35.
\+\.{arithmetic overflow...}, 72, 73.
\+\\{ascii\_code}, \[7], 9, 27, 42, 54, 57.
\+\|{b}, \[24].
\+\.{Bad DVI file}, 77.
\+\.{bad scale}, 93.
\+\\{bad\_char}, \[63], 68.
\+\\{bad\_dvi}, \[77], 80, 81, 82, 84, 86, 88, 89, 96.
\+\\{banner}, \[1], 3.
\+\\{beta}, \[31], 34, 35.
\+\\{boolean}, 31, 39, 41, 46, 63.
\+\\{bop}, 12, 14, \[15], 16, 38, 58, 61, 66, 76, 81, 82, 84, 96.
\+\.{bop occurred before eop}, 66.
\+\\{break}, 43.
\+\\{buf\_ptr}, \[45], 46, 49, 50, 51, 52.
\+\\{buffer}, \[42], 44, 45, 46, 48, 49, 50, 51, 52.
\+\\{byte\_file}, \[18], 19.
\+\\{b0}, \[22], 23, 32, 33, 34.
\+\\{b1}, \[22], 23, 32, 34.
\+\\{b2}, \[22], 23, 32, 34.
\+\\{b3}, \[22], 23, 32, 34.
\+\|{c}, \[24].
\+\\{change\_font}, \[63], 64, 65.
\+\\{char}, 8, 21, 90.
\+\\{char\_pixel\_width}, \[36], 70.
\+\\{char\_width}, \[27], 36, 70.
\+\\{char\_width\_end}, \[27], 36.
\+\.{character $c$ invalid...}, 70.
\+{check sum}, 16.
\+{Chinese characters}, 14, 27.
\+\\{chr}, 8, 9, 11.
\+\\{conv}, \[36], 37, 62, 86.
\+\\{count}, \[39], 41, 81, 83.
\+\\{cur\_font}, \[63], 67, 70, 75.
\+\\{cur\_loc}, 20, \[21], 24, 25, 64, 81, 82, 83, 84, 87, 95, 96.
\+\\{cur\_name}, 20, \[21], 92.
\+\\{cur\_pos}, 25.
\+\|{d}, \[24].
\+\\{decr}, \[6], 66, 80, 89, 96.
\+\.{deeper than claimed...}, 66.
\+\\{default\_directory}, \[90], 91, 92.
\+\\{default\_directory\_name}, \[90], 91.
\+\\{default\_directory\_name\_length}, \[90], 92.
\+\\{dialog}, \[47], 95.
\+\\{do\_nothing}, \[6].
\+\\{do\_page}, 58, 61, \[63], 65, 66, 96.
\+\\{done}, \[63], 64, 65, 66, 70, 71, 72, 73, 74.
\+\\{down1}, 14, \[15], 61, 67.
\+\\{down2}, 14.
\+\\{down3}, 14.
\+\\{down4}, 14.
\+\.{DVI {\rm files}}, 12.
\+\\{dvi\_file}, \[3], \[19], 20, 21, 24, 25, 82, 87, 88.
\+\\{dvi\_length}, \[25], 80.
\+\\{DVI\_type}, \[3].
\+\.{DVItype capacity exceeded...}, 88, 89.
\+\.{DVItype needs larger...}, 32.
\+\\{eight\_bits}, \[18], 22, 24, 61, 63.
\+\\{eight\_cases}, \[61].
\+\\{eof}, 20, 24, 25, 32, 82, 87, 88, 93.
\+\\{eoln}, 44.
\+\\{eop}, 12, 14, \[15], 38, 61, 66.
\+\\{error}, \[64], 65, 66, 68, 70, 72, 73.
\+\\{errors\_only}, \[38], 53, 56, 64.
\+\|{f}, \[29].
\+\\{false}, 2, 17, 31, 39, 40, 41, 46, 49, 63, 64, 68, 82.
\+\\{fin\_rule}, \[63], 64, 65.
\+\\{fin\_set}, \[63], 64, 65, 69.
\+\\{final\_end}, \[4], 77, 95, 96.
\+\\{first\_par}, \[61], 64, 65, 82.
\+\\{first\_text\_char}, \[8], 11.
\+\\{fix\_word}, 34.
\+\\{flush\_text}, \[56], 57, 64.
\+\\{fnt\_num\_0}, 14, \[15], 61, 65.
\+\\{fnt\_num\_1}, 14.
\+\\{fnt\_num\_63}, 14.
\+\\{fnt1}, 14, \[15], 61, 65.
\+\\{fnt2}, 14.
\+\\{fnt3}, 14.
\+\\{fnt4}, 14.
\+\.{Font name is too long}, 92.
\+\\{font\_bc}, \[27], 32, 37, 70.
\+\\{font\_ec}, \[27], 32, 70.
\+\\{font\_name}, \[27], 28, 29, 89, 92.
\+\\{font\_num}, \[27], 75, 88, 93.
\+\\{font\_space}, \[27], 67, 94.
\+\\{four\_cases}, \[61], 65, 67.
\+\\{get}, 44.
\+\\{get\_byte}, \[24], 25, 61, 64, 68, 80, 81, 82, 87, 89, 95, 96.
\+\\{get\_integer}, \[46], 49, 50, 51, 52.
\+\\{get\_three\_bytes}, \[24], 61.
\+\\{get\_two\_bytes}, \[24], 61, 85.
\+\|{h}, \[59].
\+\\{hh}, \[59], 63, 66, 67, 70, 71, 72, 74.
\+\\{hhstack}, \[59], 66.
\+\\{hstack}, \[59], 66.
\+\|{i}, \[3].
\+\\{id\_byte}, \[17], 80, 87.
\+\.{identification...should be...}, 87.
\+\\{in\_TFM}, \[31], 34, 93.
\+\\{in\_width}, \[30], 34, 37.
\+\\{incr}, \[6], 24, 44, 46, 49, 51, 57, 66, 75, 81, 83, 89, 92, 93, 94.
\+\\{infinity}, \[72], 73.
\+\\{initialize}, \[3], 95.
\+\\{input\_ln}, 42, \[44], 48, 49, 50, 51, 52.
\+\\{integer}, 3, 18, 21, 24, 25, 27, 29, 30, 31, 36, 38, 39, 46, 47, 59, 60,
61, 62, 63, 76, 78.
\+\\{invalid\_width}, \[27], 70.
\+{Japanese characters}, 14, 27.
\+\|{k}, \[29], \[31], \[41], \[44], \[47], \[56], \[63], \[78].
\+\\{last\_text\_char}, \[8], 11.
\+\\{lh}, \[31], 32.
\+\\{line\_length}, \[5], 54, 56, 57.
\+\|{m}, \[78].
\+\\{major}, \[64], 65, 66, 67, 68, 69.
\+\\{match}, \[41].
\+\\{max\_fonts}, \[5], 27, 88.
\+\\{max\_h}, \[60], 72, 85.
\+\\{max\_pages}, \[38], 40, 50, 53, 96.
\+\\{max\_stack\_depth}, \[60], 66, 85.
\+\\{max\_v}, \[60], 73, 85.
\+\\{max\_widths}, \[5], 27, 31, 32, 36.
\+\\{minor}, \[64], 66, 67, 69.
\+\\{mn}, 16.
\+\\{move\_down}, \[63], 64, 67.
\+\\{move\_right}, \[63], 64, 67, 70, 71.
\+\\{move\_to\_byte}, \[25], 79, 80, 81, 95.
\+\|{n}, \[62], \[78].
\+\\{name\_length}, \[5], 21, 92.
\+\\{name\_size}, \[5], 27, 29, 89.
\+\\{names}, \[27], 29, 89, 92.
\+\\{negative}, \[46].
\+\\{new\_mag}, \[38], 52, 53, 86.
\+\\{nf}, \[27], 28, 29, 32, 37, 63, 70, 75, 88, 89, 92, 93, 94.
\+\.{non-ascii character...}, 68.
\+\\{nop}, 12, 14, \[15], 61, 66, 81, 82, 96.
\+\.{not enough signature bytes...}, 87.
\+\\{nw}, \[31], 32, 33, 34.
\+\|{o}, \[63].
\+\\{open\_dvi\_file}, \[20], 79, 95.
\+\\{open\_tfm\_file}, \[20], 21, 93.
\+\\{ord}, 9.
\+{oriental characters}, 14, 27.
\+\\{out\_mode}, \[38], 40, 48, 53, 56, 64.
\+\\{out\_space}, \[67].
\+\\{out\_text}, \[57], 67, 69.
\+\\{out\_vmove}, \[67].
\+\\{output}, \[3].
\+\|{p}, \[63], \[78].
\+\.{page ended unexpectedly}, 96.
\+\\{page\_count}, \[76], 79, 81, 83, 85.
\+\\{pixel\_round}, \[37], 59, 67.
\+\\{pixel\_width}, \[36], 37.
\+\\{pop}, 13, 14, \[15], 16, 61, 66.
\+\\{print}, \[3], 29, 53, 56, 64, 68, 70, 71, 72, 73, 74, 75, 77, 85, 88, 96.
\+\\{print\_font}, \[29], 70, 75, 89.
\+\\{print\_ln}, \[3], 31, 32, 53, 56, 63, 64, 74, 85, 86, 87, 93, 94, 95, 96.
\+\\{pst}, 12, 14, \[15], 16, 17, 61, 65, 76, 80, 81, 82, 85, 96.
\+\.{pst occurred before eop}, 65.
\+\.{pst pointer...should be...}, 87.
\+\\{pst\_loc}, \[76], 79, 80, 81, 82, 85, 87.
\+\\{push}, 5, 13, 14, \[15], 16, 61, 66.
\+\.{push deeper than claimed...}, 66.
\+\\{put\_rule}, 14, \[15], 61, 65, 71, 82.
\+\\{put1}, 14, \[15], 61, 65, 70.
\+\\{put2}, 14.
\+\\{put3}, 14.
\+\\{put4}, 14.
\+\|{q}, \[63], \[78].
\+\|{r}, \[78].
\+\\{random\_reading}, \[2], 17, 25, 76, 79, 82, 95.
\+\\{read}, 23, 24.
\+\\{read\_ln}, 44.
\+\\{read\_tfm\_word}, \[23], 32, 33, 34.
\+\\{real}, 36, 38.
\+\\{reset}, 20, 44.
\+\\{resolution}, \[38], 51, 53, 86.
\+\\{rewrite}, 47.
\+\\{right1}, 14, \[15], 61, 67.
\+\\{right2}, 14.
\+\\{right3}, 14.
\+\\{right4}, 14.
\+\\{rule\_pixels}, 14, \[62], 71.
\+\|{s}, \[63].
\+\\{set\_char\_0}, 14, \[15], 61, 65.
\+\\{set\_char\_1}, 14.
\+\\{set\_char\_127}, 14.
\+\\{set\_pos}, 25.
\+\\{set\_rule}, 12, 14, \[15], 61, 65, 82.
\+\\{set1}, 14, \[15], 61, 65.
\+\\{set2}, 14.
\+\\{set3}, 14.
\+\\{set4}, 14.
\+\\{show}, \[64].
\+\\{show\_state}, \[63], 64, 66.
\+\\{showing}, \[63], 64, 68, 71, 72, 73, 74, 75.
\+\.{signature...should be...}, 87.
\+\\{signed\_byte}, \[24], 61.
\+\\{signed\_pair}, \[24], 61.
\+\\{signed\_quad}, \[24], 61, 71, 80, 81, 82, 83, 84, 85, 86, 87, 88, 96.
\+\\{signed\_trio}, \[24], 61.
\+\\{sixteen\_cases}, \[61].
\+\\{sixty\_four\_cases}, \[61], 65.
\+\\{ss}, \[63], 66, 74.
\+\.{stack not empty...}, 66.
\+\\{stack\_size}, \[5], 59, 66.
\+\\{start\_count}, \[39], 41, 49, 53.
\+\\{start\_loc}, \[76], 79, 81, 83, 95.
\+\\{start\_match}, \[41], 81, 83.
\+\\{start\_there}, \[39], 40, 41, 49, 53.
\+\\{start\_vals}, \[39], 40, 41, 49, 53, 96.
\+{system dependencies}, \[2], 8, 17, 18, 20, 23, 24, 25, 38, 42, 43, 44, 47,
90, 92, 97.
\+\\{term\_in}, \[42], 44.
\+\\{term\_out}, \[42], 43, 47, 48, 49, 50, 51, 52.
\+\\{terminal\_line\_length}, \[5], 42, 44, 45.
\+\\{terse}, \[38], 53.
\+\\{text\_buf}, \[54], 56, 57.
\+\\{text\_char}, \[8], 9.
\+\\{text\_file}, \[8], 42.
\+\\{text\_ptr}, \[54], 55, 56, 57.
\+\.{TFM {\rm files}}, 26.
\+\.{TFM file can\'t be opened}, 93.
\+\.{TFM file is bad}, 31.
\+\\{tfm\_check\_sum}, \[30], 32, 94.
\+\\{tfm\_file}, \[19], 20, 23, 30, 32, 93.
\+\\{thirty\_two\_cases}, \[61].
\+\.{this number already used}, 93.
\+\\{true}, 2, 25, 31, 39, 41, 46, 49, 63, 64, 66, 68.
\+\\{trunc}, 37, 62.
\+\.{undefined command}, 65.
\+\.{undefined font}, 29.
\+\\{undefined\_commands}, \[15], 61, 65.
\+\\{update\_terminal}, \[43], 44.
\+\|{v}, \[59].
\+\\{verbose}, \[38], 40, 53, 64.
\+\\{vstack}, \[59], 66.
\+\\{vv}, \[59], 63, 66, 67, 73, 74.
\+\\{vvstack}, \[59], 66.
\+\|{w}, \[59].
\+\.{warning:...}, 72, 73.
\+\\{width}, \[27], 33, 36, 37.
\+\\{width\_base}, \[27], 36, 37.
\+\\{width\_ptr}, \[27], 28, 31, 32, 33, 37.
\+\\{wp}, \[31], 32, 33, 37.
\+\\{write}, 3, 48, 49, 50, 51, 52.
\+\\{write\_ln}, 3, 48, 49, 50, 51, 52.
\+\\{wstack}, \[59], 66.
\+\\{w0}, 14, \[15], 61, 67.
\+\\{w1}, 14, \[15], 61, 67.
\+\\{w2}, 14.
\+\\{w3}, 14.
\+\\{w4}, 14.
\+\|{x}, \[46], \[59].
\+\\{xchr}, \[9], 10, 11, 29, 56, 68, 92.
\+\\{xord}, \[9], 11, 44.
\+\\{xstack}, \[59], 66.
\+\\{xxx}, 14, \[15], 61, 63, 65, 82.
\+\\{x0}, 14, \[15], 61, 67.
\+\\{x1}, 14, \[15], 61, 67.
\+\\{x2}, 14.
\+\\{x3}, 14.
\+\\{x4}, 14.
\+\|{y}, \[59].
\+\\{ystack}, \[59], 66.
\+\\{y0}, 14, \[15], 61, 67.
\+\\{y1}, 14, \[15], 61, 67.
\+\\{y2}, 14.
\+\\{y3}, 14.
\+\\{y4}, 14.
\+\|{z}, \[31], \[59].
\+\\{zstack}, \[59], 66.
\+\\{z0}, 14, \[15], 61, 67.
\+\\{z1}, 14, \[15], 61, 67.
\+\\{z2}, 14.
\+\\{z3}, 14.
\+\\{z4}, 14.
\fin
\+\X67:Cases for horizontal and vertical motion\X
\+\X66:Cases for \\{nop}, \\{bop}, $\ldotss$, \\{pop}\X
\+\X84:Check the pointer to the previous \\{bop}\X
\+\X86:Compute the conversion factor\X
\+\X5:Constants in the outer block\X
\+\X81:Count the pages and find \\{start\_loc}\X
\+\X50:Determine the desired \\{max\_pages}\X
\+\X52:Determine the desired \\{new\_mag}\X
\+\X48:Determine the desired \\{out\_mode}\X
\+\X51:Determine the desired \\{resolution}\X
\+\X49:Determine the desired \\{start\_count} values\X
\+\X79:Do a quick look at the file, moving to byte number $\\{pst\_loc}+5$\X
\+\X80:Find \\{pst\_loc}, working backwards from the end of the file\X
\+\X75:Finish a command that changes the current font\X
\+\X70:Finish a command that either sets or puts a character, then \&{goto} \\
{move\_right} or \\{done}\X
\+\X71:Finish a command that either sets or puts a rule, then \&{goto} \\{move\_
right} or \\{done}\X
\+\X72:Finish a command that sets $\|h\K\|h+\|q$, then \&{goto} \\{done}\X
\+\X73:Finish a command that sets $\|v\K\|v+\|p$, then \&{goto} \\{done}\X
\+\X94:Finish loading the new font info\X
\+\X9:Globals in the outer block\X
\+\X82:Go through the file, counting pages and finding \\{start\_loc} and \\
{pst\_loc}\X
\+\X4:Labels in the outer block\X
\+\X93:Load the font unless there are problems\X
\+\X87:Make sure that the end of the file is well-formed\X
\+\X89:Move font name into storage and into \\{cur\_name}\X
\+\X92:Move font name into the \\{cur\_name} string\X
\+\X37:Move the widths from \\{in\_width} to \\{width}, and append \\{pixel\_
width} values\X
\+\X83:Pass \\{bop} command\X
\+\X53:Print all the selected options\X
\+\X88:Process the font definitions\X
\+\X34:Read and convert the width values, setting up the \\{in\_width} table\X
\+\X32:Read past the header data; \&{goto} 9997 if there is a problem\X
\+\X85:Read, translate, and check the postamble\X
\+\X35:Replace \|z by $\|z↑\prime$ and compute $\alpha,\beta$\X
\+\X10:Set initial values\X
\+\X74:Show the values of \\{ss}, \|h, \|v, \|w, \|x, \|y, \|z, \\{hh}, and \\
{vv}; then \&{goto} \\{done}\X
\+\X65:Start translation of command \|o and \&{goto}  the appropriate label to
finish the job\X
\+\X33:Store character-width indices at the end of the \\{width} table\X
\+\X69:Translate a \\{set\_char} command\X
\+\X68:Translate an \\{xxx} command and \&{goto} \\{done}\X
\+\X64:Translate the next command in the \.{DVI} file; \&{goto} 9999 with $\\
{do\_page}=\\{true}$ if it was \\{eop}; \&{goto} 9998 if premature termination
is needed\X
\+\X96:Translate up to \\{max\_pages} pages; \&{goto} \\{final\_end} if the
postamble is reached\X
\+\X7:Types in the outer block\X
\con